Created
August 2, 2013 19:12
-
-
Save astockwell/6142551 to your computer and use it in GitHub Desktop.
Format Date/Time strings in PHP < version 5.3
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 | |
/* | |
* VARIABLE FORMATS: | |
* $e_date = '02/10/2012' (string) | |
* $e_time = '7:00 pm' (string) | |
* $e_end = '11:00 pm' (string) | |
* $format_short = true (boolean) | |
*/ | |
function format_date_time($e_date, $e_time, $e_end = null, $format_short = null) { | |
$e_date_time = strtotime($e_date . ' ' . $e_time); | |
if ($e_end != null && $e_end != false) { | |
$e_date_time_formatted = date('l, F j | g:i', $e_date_time); | |
} else { | |
$e_date_time_formatted = date('l, F j | g:i a', $e_date_time); | |
} | |
if ($format_short != null) { | |
if ($e_end != null && $e_end != false) { | |
$e_date_time_formatted = date('l, M j | g:i', $e_date_time); | |
} else { | |
$e_date_time_formatted = date('l, M j | g:i a', $e_date_time); | |
} | |
} | |
if ($e_end != null && $e_end != false) { | |
$e_end_time_formatted = date('g:i a', strtotime($e_date . ' ' . $e_end)); | |
$e_date_time_formatted = $e_date_time_formatted . "–" . $e_end_time_formatted; | |
} | |
$s = array(' am', ' AM', ' pm', ' PM'); | |
$r = array(' a.m.', ' A.M.', ' p.m.', ' P.M.'); | |
return str_replace($s, $r, $e_date_time_formatted); | |
/* OLD FUNCTION FOR PHP >= 5.3 | |
$e_date_time = date_create_from_format('m/d/Y g:i a', $e_date . " " . $e_time); | |
$e_date_time_formatted = date_format($e_date_time, 'l, F j | g:i a'); | |
if ($format_short != null) $e_date_time_formatted = date_format($e_date_time, 'l, M j | g:i a'); //'D m/d/Y | g:i a' | |
if ($e_end != null) { | |
$e_end_time = date_create_from_format('g:i a', $e_end); | |
$e_end_time_formatted = date_format($e_end_time, 'g:i a'); | |
$e_date_time_formatted = $e_date_time_formatted . "–" . $e_end_time_formatted; | |
} | |
$s = array(' am', ' AM', ' pm', ' PM'); | |
$r = array(' a.m.', ' A.M.', ' p.m.', ' P.M.'); | |
return str_replace($s, $r, $e_date_time_formatted); | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment