3v4l.org

run code in 300+ PHP versions simultaneously
<?php $dates = [ '2010-12-30', '2011-01-01', '2011-01-02', '2011-01-03', '2011-01-06', '2011-01-06', '2011-01-07', '2011-01-10', ]; function formatRange(object $start, ?object $end = null): string { $last = $end ?? $start; return sprintf( '%s%s', $start->format('l j M'), $last === $start ? '' : ' to ' . $last->format('l j M') ); } $result = []; foreach ($dates as $date) { $obj = new Datetime($date); if (isset($temp['start'])) { if ($obj->diff($temp['end'] ?? $temp['start'])->days > 1) { $result[] = formatRange(...$temp); $temp = ['start' => $obj]; } else { $temp['end'] = $obj; } } else { $temp['start'] = $obj; } } if (isset($temp)) { $result[] = formatRange(...$temp); } var_export($result);

preferences:
25.82 ms | 405 KiB | 5 Q