3v4l.org

run code in 300+ PHP versions simultaneously
<?php $start = new \DateTime('first day of this month'); $end = (new \DateTime('first day of this month'))->modify('+18 months'); $interval = new \DateInterval('P1M'); $period = new \DatePeriod($start, $interval, $end); foreach ($period as $month) { $lastDayOfMonth = $month->format('t'); $day = (date('d') > $lastDayOfMonth) ? $lastDayOfMonth : date('d'); echo $month->format("n | Y | Y-m-{$day}"); echo "\n"; }

preferences:
123.39 ms | 404 KiB | 5 Q