diff options
author | Thomas Lange <code@nerdmind.de> | 2016-01-09 20:44:22 +0100 |
---|---|---|
committer | Thomas Lange <code@nerdmind.de> | 2016-01-09 20:44:22 +0100 |
commit | 49ae01e05ef5a68ad0d22f5b73992149409434a2 (patch) | |
tree | b4b8f69ab1ddec2e18a82b4a40f71ac8e07a3a44 /PHP/functions/parseDatetime.php | |
parent | a6a6cd0bd34a6a4f7c25b901df1bd1d022b1683c (diff) | |
download | snippets-49ae01e05ef5a68ad0d22f5b73992149409434a2.tar.gz snippets-49ae01e05ef5a68ad0d22f5b73992149409434a2.tar.xz snippets-49ae01e05ef5a68ad0d22f5b73992149409434a2.zip |
Initial commit
Diffstat (limited to 'PHP/functions/parseDatetime.php')
-rw-r--r-- | PHP/functions/parseDatetime.php | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/PHP/functions/parseDatetime.php b/PHP/functions/parseDatetime.php new file mode 100644 index 0000000..bce498b --- /dev/null +++ b/PHP/functions/parseDatetime.php @@ -0,0 +1,51 @@ +<?php +#=============================================================================== +# FUNCTION: Parser for timestamps [YYYY-MM-DD HH:II:SS] +#=============================================================================== +function parseDatetime($datetime, $format) { + list($datepart, $timepart) = explode(' ', $datetime); + list($year, $month, $day) = explode('-', $datepart); + list($hour, $minute, $second) = explode(':', $timepart); + + $months = [ + '01' => 'January', + '02' => 'February', + '03' => 'March', + '04' => 'April', + '05' => 'May', + '06' => 'June', + '07' => 'July', + '08' => 'August', + '09' => 'September', + '10' => 'October', + '11' => 'November', + '12' => 'December' + ]; + + $days = [ + 0 => 'Sunday', + 1 => 'Monday', + 2 => 'Tuesday', + 3 => 'Wednesday', + 4 => 'Thursday', + 5 => 'Friday', + 6 => 'Saturday' + ]; + + return strtr($format, [ + '[Y]' => $year, + '[M]' => $month, + '[D]' => $day, + '[H]' => $hour, + '[I]' => $minute, + '[S]' => $second, + '[W]' => $days[date('w', strtotime($datetime))], + '[F]' => $months[date('m', strtotime($datetime))] + ]); +} + +#=============================================================================== +# EXAMPLE: +#=============================================================================== +echo parseDatetime('2015-10-25 12:24:32', '[W], [D]. [F] [Y] at [H]:[M]'); +?>
\ No newline at end of file |