Skip to content

Instantly share code, notes, and snippets.

@chappy84
Last active December 11, 2015 01:18
Show Gist options
  • Save chappy84/4522039 to your computer and use it in GitHub Desktop.
Save chappy84/4522039 to your computer and use it in GitHub Desktop.
Format a local time/date using an ISO Format
<?php
function date_iso($format, $timestamp = null)
{
if ($timestamp === null) {
$timestamp = time();
}
$convert = array(
'a' => 'A' , 'B' => 'B', 'D' => 'z', 'ddd' => 't', 'dd' => 'd', 'd' => 'j',
'EEEE' => 'l', 'EE' => 'D', 'eee' => 'N', 'e' => 'w', 'HH' => 'H', 'H' => 'G',
'hh' => 'h', 'h' => 'g', 'I' => 'I', 'l' => 'L', 'MMMM' => 'F', 'MMM' => 'M',
'MM' => 'm', 'M' => 'n', 'mm' => 'i', 'r' => 'r', 'SS' => 'S', 'ss' => 's',
'U' => 'U', 'ww' => 'W', 'X' => 'Z', 'YYYY' => 'o', 'yyyy' => 'Y', 'yy' => 'y',
'ZZZZ' => 'P', 'zzzz' => 'e', 'Z' => 'O', 'z' => 'T'
);
$values = preg_split(
'/(a|B|D|d{1,3}|EEEE|EE|eee|e|HH|H|hh|h|I|l|M{1,4}|mm|r|SS|ss|U|ww|X|YYYY|yyyy|yy|ZZZZ|zzzz|Z|z|[^a-zA-Z]+)/',
$format,
-1,
PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);
foreach ($values as $key => $value) {
if (isset($convert[$value])) {
$values[$key] = date($convert[$value], $timestamp);
}
}
return implode($values);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment