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']]; $common = array_map(fn($timeRange) => explode('-', $timeRange), array_shift($openTimeslots)); foreach ($openTimeslots as $slots) { foreach ($slots as $slot) { [$start, $end] = explode('-', $slot); foreach ($common as [&$commonStart, &$commonEnd]) { if ($start >= $commonStart && $start < $commonEnd) { $commonStart = $start; } if ($end > $commonStart && $end <= $commonEnd) { $commonEnd = $end; } } } } var_export( array_map( fn($subarray) => implode('-', $subarray), $common ) );

preferences:
96.32 ms | 408 KiB | 5 Q