Azyrus Posted May 1, 2012 Share Posted May 1, 2012 Hi everyone I have a column in an sql database which stores time in minutes. I'm trying to turn these minutes into this kind of format: 1:00pm 12:30am 6:15pm I looked on W3C and there was the %r which seemed to format this way but I cant get it to work >.< $time1=$f3/60; $time2= date_format($f4, '%r'); Does anyone know how to achieve this? THANKS!!! Quote Link to comment https://forums.phpfreaks.com/topic/261888-time-format/ Share on other sites More sharing options...
smerny Posted May 1, 2012 Share Posted May 1, 2012 g:ia Quote Link to comment https://forums.phpfreaks.com/topic/261888-time-format/#findComment-1341915 Share on other sites More sharing options...
Jessica Posted May 1, 2012 Share Posted May 1, 2012 In the future, use php.net as your resource. http://php.net/date Quote Link to comment https://forums.phpfreaks.com/topic/261888-time-format/#findComment-1341925 Share on other sites More sharing options...
Azyrus Posted May 1, 2012 Author Share Posted May 1, 2012 Hi guys, Thanks so much for your help I'll try my best to remember to use php.net from now on! The format is perfect but I think I need to do something to the time value. In the database seems to store time in minutes the value for 6am is an integer of 360, 7am would be 420 and so on... (This is my understanding which could be wrong). Using the code below: <?php echo date("g:ia","$f4"); ?> The output for 360 which is 6am = 1:06am. Does anyone know how to do this? Thanks a lot! I really appreciate your help guys Quote Link to comment https://forums.phpfreaks.com/topic/261888-time-format/#findComment-1342121 Share on other sites More sharing options...
xyph Posted May 1, 2012 Share Posted May 1, 2012 Use simple math functions. The % (modulus) returns the remainder of a division operation. <?php $time = 799; $hour = floor($time/60); $min = str_pad($time % 60,2,0,STR_PAD_LEFT); echo $hour.':'.$min; echo '<br><br>'; // For AM/PM $half = 'AM'; if( $hour > 12 ) { $half = 'PM'; $hour -= 12; } echo $hour.':'.$min.' '.$half; ?> Quote Link to comment https://forums.phpfreaks.com/topic/261888-time-format/#findComment-1342126 Share on other sites More sharing options...
Azyrus Posted May 1, 2012 Author Share Posted May 1, 2012 Thank you very much xyph! It looks so technical! I would never have got it myself... Quote Link to comment https://forums.phpfreaks.com/topic/261888-time-format/#findComment-1342143 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.