rsammy Posted April 2, 2007 Share Posted April 2, 2007 i have time stored in a string $visit_time it comes in as "2:15 PM" or "10:13 AM". i am trying to convert this to 24 hour format... say 14:15 or 10:13 depending on AM or PM in the string. i am trying to split this string as: list($hour, $min)=split('[:]', $visit_time); if (strlen($hour)==1) { $visit_time_reformat="0".$hour; } if (strlen($min)==1) { $visit_time_reformat=$visit_time_reformat.":".$min; } now, how can i get rid of "AM" or "PM" and display the time($visit_time_reformat) in 24 hour format? Quote Link to comment https://forums.phpfreaks.com/topic/45281-solved-conversion-of-time-string/ Share on other sites More sharing options...
monk.e.boy Posted April 2, 2007 Share Posted April 2, 2007 list($hour, $tmp)=split('[:]', $visit_time); list($min, $mm) = split('[\s]', $tmp); if ($mm == 'AM') { $hour= int_val($hour)+12; } Hows that? monk.e.boy Quote Link to comment https://forums.phpfreaks.com/topic/45281-solved-conversion-of-time-string/#findComment-219864 Share on other sites More sharing options...
kenrbnsn Posted April 2, 2007 Share Posted April 2, 2007 You really should use the functions in PHP that deal with date/time like strtotime() and date() <?php $time = '2:15 PM'; $time24 = date('H:i',strtotime($time)); echo $time . ' ==> ' . $time24; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/45281-solved-conversion-of-time-string/#findComment-219891 Share on other sites More sharing options...
rsammy Posted April 2, 2007 Author Share Posted April 2, 2007 thanx a lot for ur replies guys! it helped. Quote Link to comment https://forums.phpfreaks.com/topic/45281-solved-conversion-of-time-string/#findComment-220176 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.