V Posted June 2, 2012 Share Posted June 2, 2012 I'm having trouble rounding time For example I have 01:08:34 and I want to round it to the nearest quarter (and eliminate seconds). I should get 01:00:00 or 01:41:00 to 01:45:00 I'm using the following code but it doesn't work $time = strtotime("01:08:34"); $round = 15; $rounded = (round($time * 60 / $round) * $round) / 60; echo gmdate("H:i:00", $rounded); The output is 01:08:00, the same time not rounded. Please advise me how to do this right. Link to comment https://forums.phpfreaks.com/topic/263556-round-time-to-nearest-quarter/ Share on other sites More sharing options...
tipsmail7 Posted June 3, 2012 Share Posted June 3, 2012 Maybe you can change the formula in the 3rd line to $rounded = (round($time / $round / 60)) * $round * 60; Link to comment https://forums.phpfreaks.com/topic/263556-round-time-to-nearest-quarter/#findComment-1350727 Share on other sites More sharing options...
Barand Posted June 3, 2012 Share Posted June 3, 2012 <?php $time = strtotime("01:08:34"); echo $time.'<br />'; $round = 15*60; $rounded = round($time / $round) * $round; echo date("H:i:00", $rounded); ?> Link to comment https://forums.phpfreaks.com/topic/263556-round-time-to-nearest-quarter/#findComment-1350817 Share on other sites More sharing options...
V Posted June 4, 2012 Author Share Posted June 4, 2012 Thanks Barand! The code works nicely Link to comment https://forums.phpfreaks.com/topic/263556-round-time-to-nearest-quarter/#findComment-1350994 Share on other sites More sharing options...
silkfire Posted June 4, 2012 Share Posted June 4, 2012 Shouldn't 01:08:34 be rounded to 01:15? Link to comment https://forums.phpfreaks.com/topic/263556-round-time-to-nearest-quarter/#findComment-1351089 Share on other sites More sharing options...
Barand Posted June 4, 2012 Share Posted June 4, 2012 Yes, that's precisely what I got when I ran the code. Link to comment https://forums.phpfreaks.com/topic/263556-round-time-to-nearest-quarter/#findComment-1351090 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.