Divide time into intervals

I have three inputs

  • Start time
  • End time
  • intermediate time.

Example

start time - 01:00 end time - 01:30 intervel-time - 10 min 

I need a conclusion, for example, 01:00, 01:10, 01:20, 01:30

I tried this code below, its not working.

 <?php $startTime=strtotime("2012-07-13 01:00:00"); $endTime=strtotime("2012-07-13 01:30:00"); $time=$startTime; while ($time < $endTime) { echo date('H:i:s', $time) . " - "; $time = strtotime('+10 minutes', $time); echo date('H:i:s', $time) . "<br>"; } ?> 

When I try with a start and end time interval of 60 minutes, the above code works.

I am new to PHP.

Someone please help me.

+6
source share
6 answers

Try the following:

 $startTime = new DateTime("2012-07-13 01:00:00"); $endTime = new DateTime("2012-07-13 01:30:00"); while ($startTime < $endTime) { echo $startTime->format('H:i:s') . ' - '; echo $startTime->modify('+10 minutes')->format('H:i:s') . "\n"; } 

demonstration

+3
source

Try this with DateTime and DateInterval:

 <?php $startTime = new DateTime('2012-07-13 01:00:00'); $endTime = new DateTime('2012-07-13 01:30:00'); while($startTime <= $endTime) { echo $startTime->format('H:i:s') . ' '; $startTime->add(new DateInterval('PT10M')); } ?> 

Eval-demo

+3
source

Your code works very well, I just added a variable that indicates the interval and reformatted the time so that it is the same as I need the output like 01:00, 01:10, 01:20, 01:30 , and last, that you must first print the time it increases.

Here is the code:

 $startTime=strtotime("2012-07-13 01:00:00"); $endTime=strtotime("2012-07-13 01:30:00"); $interval = "10"; $time=$startTime; while ($time <= $endTime) { echo date('H:i', $time) . "<br>"; $time = strtotime('+'.$interval.' minutes', $time); } 
+2
source

I think the code works as expected ... Perhaps you could reorganize it to get the exact result you need:

 <?php $startTime=strtotime("2012-07-13 01:00:00"); $endTime=strtotime("2012-07-13 01:30:00"); $intervel="60"; $time=$startTime; echo date('H:i', $time); $time = strtotime('+'.$intervel.' minutes', $time); while ($time <= $endTime) { echo "," . date('H:i', $time); $time = strtotime('+'.$intervel.' minutes', $time); } ?> 

If you change from 10 to 60 intervals, then it will print only the start time, because start_time + 60 is longer than the end time; Is this what you expect?

+2
source

You can achieve this with modify , DateInterval and DatePeriod .

 $startTime = new DateTime('2012-07-13 01:00:00'); $endTime = new DateTime('2012-07-13 01:30:00'); $endTime = $endTime->modify( '+1 minute' ); $interval = new DateInterval('PT10M'); $daterange = new DatePeriod($startTime, $interval ,$endTime); foreach($daterange as $res) { echo $res->format('H:i') .", "; } 
+2
source
 $start = DateTime::createFromFormat('Ymd H:i:s', '2012-07-13 01:00:00', new DateTimeZone('Europe/Berlin')); $end = DateTime::createFromFormat('Ymd H:i:s', '2012-07-13 01:30:00', new DateTimeZone('Europe/Berlin')); while($start <= $end) { echo $start->format('H:i:s'), "\n"; $start->modify('+10 minutes'); } 

You did not have the last possible conclusion, as this would give the same meaning as your final time. a < b should have been a <= b

Result: http://3v4l.org/8eK3O

+1
source

Source: https://habr.com/ru/post/971703/


All Articles