3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ '2020-02-07' => ['09:00' => 1500, '10:30' => 1500, '11:00' => 1500, '11:30' => 0], '2020-02-08' => ['09:00' => 1500, '09:30' => 1500, '11:00' => 1500], ]; $result = []; $f = fn($p) => date('H:i', $p); foreach ($array as $k => $v) { $v = array_map(fn($a) => strtotime($a), array_keys($v)); for ($i = 0; $i < count($v); $i++) { [$a, $b] = [$v[$i], &$a]; while (isset($v[$i + 1]) && $v[$i + 1] - $v[$i] === 1800) [$b = $v[$i + 1], $i++]; $result[] = ['date' => $k, 'range' => $a == $b ? $f($a) : $f($a) .' - '. $f($b)]; } } print_r($result);
Output for 7.4.0 - 7.4.13, 8.0.0
Array ( [0] => Array ( [date] => 2020-02-07 [range] => 09:00 ) [1] => Array ( [date] => 2020-02-07 [range] => 10:30 - 11:30 ) [2] => Array ( [date] => 2020-02-08 [range] => 09:00 - 09:30 ) [3] => Array ( [date] => 2020-02-08 [range] => 11:00 ) )
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.24
Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in /in/qjNjT on line 9
Process exited with code 255.