Jump to content

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

?> 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.