Hooker Posted July 13, 2012 Share Posted July 13, 2012 Hello, I'm having a little trouble deciding how to go about splitting an amount of time into intervals, i wan't to be able to set in a start and end time and split them down into 2 hour chunks so: Start: 14:00 End: 24:00 Intervals: 14:00 - 16:00, 16:00 - 18:00, 18:00 - 20:00, 20:00 - 22:00, 22:00 - 24:00 ready to be logged into a mysql database. Can anyone set me in the right direction? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/265643-split-time-into-intervals/ Share on other sites More sharing options...
requinix Posted July 13, 2012 Share Posted July 13, 2012 start hour = 14, end hour = 24, interval hours = 2 interval start hour = start hour while start hour interval end hour = min(interval start hour + interval hours, end hour) // assuming the same day interval = [interval start hour : minute, interval end hour : minute] save interval interval start hour = interval end hour loop Or a for loop. Quote Link to comment https://forums.phpfreaks.com/topic/265643-split-time-into-intervals/#findComment-1361421 Share on other sites More sharing options...
Hooker Posted July 13, 2012 Author Share Posted July 13, 2012 <?php $firstTime=strtotime("2012-07-13 14:00:00"); $lastTime=strtotime("2012-07-13 20:00:00"); $time=$firstTime; while ($time < $lastTime) { echo date('H:i:s', $time) . " - "; $time = strtotime('+2 hours', $time); echo date('H:i:s', $time) . "<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265643-split-time-into-intervals/#findComment-1361431 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.