<?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'
],
'05-09-2021' => [
'8:15-8:45',
'9:15-9:45',
'16:15-19:00',
]
];
function slotToInts($slot) {
return sscanf(str_replace(':', '', $slot), "%d-%d");
}
$commons = array_map('slotToInts', array_shift($openTimeslots));
foreach ($openTimeslots as $slots) {
$newCommons = [];
foreach ($slots as $slot) {
[$start, $end] = slotToInts($slot);
foreach ($commons as [$commonStart, $commonEnd]) {
$newCommonStart = null;
$newCommonEnd = null;
if ($start >= $commonStart && $start < $commonEnd) {
$newCommonStart = $start;
}
if ($end > $commonStart && $end <= $commonEnd) {
$newCommonEnd = $end;
}
if ($newCommonStart || $newCommonEnd) {
$newCommons[] = [
$newCommonStart ?? $commonStart,
$newCommonEnd ?? $commonEnd
];
}
}
}
$commons = $newCommons;
}
var_export(
array_map(
fn($subarray) => implode('-', preg_replace('~(?=\d{2}(?:-|$))~', ':', $subarray)),
$commons
)
);
preferences:
23.39 ms | 409 KiB | 5 Q