3v4l.org

run code in 300+ PHP versions simultaneously
<?php $dates_array = ['20241203', '20240115', '20240116', '20240228', '20240229', '20240301']; foreach ($dates_array as $date) { $datetimes[] = new DateTime($date); } foreach ($datetimes as $key=>$date) { $dateStr = $date->format('j'); //check if there's another entry after this one if (isset($datetimes[$key+1])) { //check the if following entry is in the same month and year as this one if ($datetimes[$key+1]->format("ym") == $date->format("ym")) { $dateStr .= ","; } else { $dateStr .= " ".$date->format("F")." & "; } } else { $dateStr .= " ".$date->format("F"); } $date_array[]= $dateStr; } $date_string = join( ' ', $date_array ) ; echo $date_string;

preferences:
37.68 ms | 404 KiB | 5 Q