3v4l.org

run code in 300+ PHP versions simultaneously
<?php $hours = ["08:00:00", "18:00:00"]; $bookings = [["10:00:00", "15:00:00"], ["08:05:00", "09:00:00"], ["16:00:00", "17:00:00"] ]; $start = $hours[0]; $end = $hours[1]; // sort the bookings $bstart = array_column($bookings, 0); array_multisort($bstart, $bookings); // get the free times $time = $start; $bindex = 0; $free = array(); while ($time < $end && $bindex < count($bookings)) { if ($time < $bookings[$bindex][0]) { $free[] = [$time, $bookings[$bindex][0]]; } $time = $bookings[$bindex][1]; $bindex++; } // end of day free? if ($time < $end) { $free[] = [$time, $end]; } print_r($free);

preferences:
33.23 ms | 402 KiB | 5 Q