Last active
December 11, 2015 01:18
-
-
Save chappy84/4522039 to your computer and use it in GitHub Desktop.
Format a local time/date using an ISO Format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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