Hello everyone, looking for a function to use to display time properly. I have multiple times stored as "$time" in the format of XXXX. Its always a 4 digit number in military time from 0001 to 2400. Is there a function to display these time properly in the format of 11:45pm instead of 1145? I could go the totally newb way and use a huge amount of if statements for the times I need (IF $time = 0600 then $newtime = 6:00am) but I swear there is a function I am overlooking.... Any ideas?
// you could always just set $hour to the first two, $minute to the last two, then if $hour > 12, $hour - 12 and set $ante = 'pm';
It works, but made a couple of changes.. if the time is in 24 hr format, and you're then converting it to have am/pm on the end.. you want it in 12 hours format function convert_time($time) { //for example, $time = "0600"; $a = substr($time, 0, 2); $b = substr($time, 2, 4); $am_or_pm = (int)$a >= 12 ? "pm" : "am"; if ($a> 12) $a-=12; $new_time = sprintf("%02d:%02d%s",$a,$b, $am_or_pm); return $new_time; } PHP: