Wednesday, March 3, 2010

Converting military 24 hour time to 12 hour time in PHP

Here is a function I use with array_walk to convert all of the values in the array to 12 hour time from military time.


function convert_to_twelve_hour(&$i) {
if($i >= 1 && $i <= 12) {
$hour = $i;
$ampm = ($i < 12) ? 'am' : 'pm';
} elseif($i < 1) {
$hour = $i + 12;
$ampm = 'am';
} elseif($i > 12) {
$hour = $i - 12;
$ampm = ($i < 12) ? 'am' : 'pm';
}
$i = $hour.$ampm;
}


This of course is an alternative to:


date("g:i a", strtotime("14:30"));

0 comments:

Post a Comment