3v4l.org

run code in 300+ PHP versions simultaneously
<?php $start = (new DateTime('1 year ago'))->modify('first day of this month'); $end = (new DateTime())->modify('first day of this month'); $interval = new DateInterval('P1M'); $period = new DatePeriod($start, $interval, $end); $months = array(); foreach ($period as $dt) { $months[$dt->format('Y-m-d')] = $dt->format('Y-m'); } $reverse_months = array_reverse($months); print_r($reverse_months);

preferences:
35.28 ms | 402 KiB | 5 Q