eldan88 Posted April 2, 2014 Share Posted April 2, 2014 Hey Guys. I am new to using dates in PHP and have been playing a lot with the PHP functions that are available for the date and time. t But I am stuck on trying to figure out, how to display the minutes of the current time, so that it will round to the nearest quarter of hour, based on the current time. Example would be: 1:15 1:30 1:45 I have a came a cross a function by doing some googling, and have came across the following function. function roundToQuarterHour($timestring) { $minutes = date('i', strtotime($timestring)); return $minutes - ($minutes % 15); } I then tried to implement doing with the following $time = time(); function roundToQuarterHour($time) { $minutes = date('i', strtotime($time)); return $minutes - ($minutes % 15); } $rounded_time = roundToQuarterHour($time); echo $rounded_time; It doesn't display anything. When i did a var_dump on the $rounded_time i got a int(0) Not sure on how to go about this....Any help would really be appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/287478-rounding-minute-to-the-nearest-quarter-of-hour/ Share on other sites More sharing options...
requinix Posted April 2, 2014 Share Posted April 2, 2014 If you got int(0) then the echo did output a 0. Somewhere. By the way, if you didn't notice that code always rounds down the minutes. 1:29 will round to 1:15. Quote Link to comment https://forums.phpfreaks.com/topic/287478-rounding-minute-to-the-nearest-quarter-of-hour/#findComment-1474747 Share on other sites More sharing options...
eldan88 Posted April 2, 2014 Author Share Posted April 2, 2014 Hey requinix. Thanks for your response. I understand it outputted a zero but, its supposed to out put the minutes. It outputs a zero regardless of what time it is. Quote Link to comment https://forums.phpfreaks.com/topic/287478-rounding-minute-to-the-nearest-quarter-of-hour/#findComment-1474749 Share on other sites More sharing options...
eldan88 Posted April 2, 2014 Author Share Posted April 2, 2014 (edited) You are right ... It does round it down. Would have any suggestion on rounding it up? Edited April 2, 2014 by eldan88 Quote Link to comment https://forums.phpfreaks.com/topic/287478-rounding-minute-to-the-nearest-quarter-of-hour/#findComment-1474750 Share on other sites More sharing options...
Barand Posted April 3, 2014 Share Posted April 3, 2014 <?php echo roundToQuarterHour('2014-04-03 10:06:33'); // 2014-04-03 10:00:00 echo roundToQuarterHour('2014-04-03 10:11:33'); // 2014-04-03 10:15:00 echo roundToQuarterHour('2014-04-03 10:18:33'); // 2014-04-03 10:15:00 echo roundToQuarterHour('2014-04-03 10:24:33'); // 2014-04-03 10:30:00 function roundToQuarterHour($tstr) { $t = round(strtotime($tstr)/900)*900; return date('Y-m-d H:i:s', $t); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/287478-rounding-minute-to-the-nearest-quarter-of-hour/#findComment-1474778 Share on other sites More sharing options...
eldan88 Posted April 4, 2014 Author Share Posted April 4, 2014 Hey Barand. Thank you so much for providing me a working example. But for future references would be able to explain me how you did the math? Just so that I can get an idea on how to implement in the future. That would be really helpful for me then just copying and pasting. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/287478-rounding-minute-to-the-nearest-quarter-of-hour/#findComment-1474912 Share on other sites More sharing options...
Barand Posted April 4, 2014 Share Posted April 4, 2014 Round the number of seconds to nearest 900 Quote Link to comment https://forums.phpfreaks.com/topic/287478-rounding-minute-to-the-nearest-quarter-of-hour/#findComment-1474923 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.