Jump to content

Split time into intervals


Hooker

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/265643-split-time-into-intervals/
Share on other sites

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.

<?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>";
}

?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.