[Solved] PHP add 45 Minutes to a Time with Loop [duplicate]


As already answered in the similar question “How can I get a range of dates in php?”, you can make use of the DatePeriod class in PHP to iterate over each 45 minute times’ between a start and end time:

Demo:

<?php
/**
 * PHP add 45 Minutes to a Time with Loop
 *
 * @link https://stackoverflow.com/a/19399907/367456
 */

$begin = new DateTime("09:00");
$end   = new DateTime("20:00");

$interval = DateInterval::createFromDateString('45 min');

$times    = new DatePeriod($begin, $interval, $end);

foreach ($times as $time) {
    echo $time->format('H:i'), '-', 
         $time->add($interval)->format('H:i'), "\n"
         ;
}

Output:

09:00-09:45
09:45-10:30
10:30-11:15
11:15-12:00
12:00-12:45
12:45-13:30
13:30-14:15
14:15-15:00
15:00-15:45
15:45-16:30
16:30-17:15
17:15-18:00
18:00-18:45
18:45-19:30
19:30-20:15

9

solved PHP add 45 Minutes to a Time with Loop [duplicate]