3v4l.org

run code in 300+ PHP versions simultaneously
<?php $openTimeslots = ['02-09-2021' => [ '8:00-10:00', '16:00-19:00'], '03-09-2021' => [ '7:00-10:00', '16:15-19:00', '14:00-16:00', '13:00-14:15'], '04-09-2021' => [ '7:15-10:00', '15:15-18:15', '18:30-18:45']]; function intersectTimeslots($timeslots1, $timeslots2) { $newTimeslots = []; foreach ($timeslots1 as $timeslot1) { [$start1, $finish1] = sscanf(str_replace(':', '', $timeslot1), "%d-%d"); foreach ($timeslots2 as $timeslot2) { [$start2, $finish2] = sscanf(str_replace(':', '', $timeslot2), "%d-%d"); $newStart = max($start1, $start2); $newFinish = min($finish1, $finish2); if ($newStart < $newFinish) { $newTimeslots[] = substr_replace($newStart, ':', -2, 0) . '-' . substr_replace($newFinish, ':', -2, 0); } } } return $newTimeslots; } $commonTimeslots = array_shift($openTimeslots); foreach ($openTimeslots as $timeslots) { $commonTimeslots = intersectTimeslots($commonTimeslots, $timeslots); } var_export($commonTimeslots);

preferences:
72.3 ms | 410 KiB | 5 Q