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' ], '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 ) );
Output for 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:15-8:45', 1 => '9:15-9:45', 2 => '16:15-18:15', )
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:15-8:45', 1 => '9:15-9:45', 2 => '16:15-18:15', )
Output for 7.3.0 - 7.3.33
Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ')' in /in/v24DG on line 56
Process exited with code 255.

preferences:
86.67 ms | 408 KiB | 5 Q