3v4l.org

run code in 300+ PHP versions simultaneously
<?php $dates = ['20240228', '20240229', '20240301']; $text = ''; foreach ($dates as $i => $date) { $date = new DateTime($date); $month = $date->format('F'); $day = $date->format('j'); $pos = strpos($text, $month); if ($pos !== false) { $text = substr_replace($text, ", $day", $pos - 1, 0); } else { $text .= ($text ? ' & ' : '') . "$day $month"; } } echo $text;
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
28, 29 February & 1 March

preferences:
75.51 ms | 402 KiB | 62 Q