@ 2021-09-20T14:57:15Z <?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 timeslotsToTimeline($timeslots)
{
$timeline = [];
foreach ($timeslots as $timeslot) {
$intTimes = sscanf(str_replace(':', '', $timeslot), "%d-%d");
$timeline[$intTimes[0]] = $intTimes[1];
}
return $timeline;
}
function timelineToTimeslots($timeline)
{
$timeslots = [];
foreach ($timeline as $start => $finish) {
$timeslots[] = intdiv($start, 100) . ':' . substr($start, -2) . '-' .
intdiv($finish, 100) . ':' . substr($finish, -2);
}
return $timeslots;
}
function intersectTimelines($timeline1, $timeline2)
{
$newTimeline = [];
foreach ($timeline1 as $start1 => $finish1) {
foreach ($timeline2 as $start2 => $finish2) {
$newStart = max($start1, $start2);
$newFinish = min($finish1, $finish2);
if ($newStart < $newFinish) {
$newTimeline[$newStart] = $newFinish;
}
}
}
return $newTimeline;
}
$commonTimeline = timeslotsToTimeline(array_shift($openTimeslots));
foreach ($openTimeslots as $timeslots) {
$newTimeline = timeslotsToTimeline($timeslots);
$commonTimeline = intersectTimelines($commonTimeline, $newTimeline);
}
$commonTimeslots = timelineToTimeslots($commonTimeline);
var_export($commonTimeslots);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.33 , 8.2.0 - 8.2.29 , 8.3.0 - 8.3.4 , 8.3.6 - 8.3.26 , 8.4.1 - 8.4.13 array (
0 => '8:00-10:00',
1 => '16:15-18:15',
2 => '18:30-18:45',
) Output for 8.3.5 Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
array (
0 => '8:00-10:00',
1 => '16:15-18:15',
2 => '18:30-18:45',
) preferences:dark mode live preview ace vim emacs key bindings
93.11 ms | 407 KiB | 5 Q