3v4l.org

run code in 300+ PHP versions simultaneously
<?php $dates = ['20240228', '20240229', '20240301']; function formatDates(array $datesInput) { foreach ($datesInput as $datesKey => $date) { $date = new DateTime($date); $month = $date->format('F'); $day = $date->format('j'); if ($datesKey == 0) { $datesOutput = $day; } elseif ($currentMonth <> $month) { $datesOutput .= " $currentMonth & $day"; } else { $datesOutput .= ", $day"; } $currentMonth = $month; } return "$datesOutput $month"; } echo formatDates($dates);
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.7
28, 29 February & 1 March

preferences:
105.45 ms | 402 KiB | 61 Q