3v4l.org

run code in 300+ PHP versions simultaneously
<?php $start = new \DateTime('2019-05-08 07:00:00'); $end = new \DateTime('2019-05-08 07:30:00'); function getSlots(\DateTime $start, \DateTime $end): array { $slots = []; while ($start < $end) { $old = clone($start); $start->modify('+2 hours'); if ($start > $end) { echo 1; $slots[] = [ 'from' => $old, 'to' => $end ]; return $slots; } else { $slots[] = [ 'from' => $old, 'to' => clone($start) ]; } } return $slots; } var_dump(getSlots($start, $end));
Output for 7.1.25 - 7.1.28, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33, 8.0.0 - 8.0.12, 8.0.14 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
1array(1) { [0]=> array(2) { ["from"]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2019-05-08 07:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } ["to"]=> object(DateTime)#2 (3) { ["date"]=> string(26) "2019-05-08 07:30:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 7.3.32 - 7.3.33, 8.0.13
1array(1) { [0]=> array(2) { ["from"]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2019-05-08 07:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } ["to"]=> object(DateTime)#2 (3) { ["date"]=> string(26) "2019-05-08 07:30:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } } }

preferences:
160.92 ms | 402 KiB | 164 Q