Use the DateTime class to find the last day of the next month:
date_default_timezone_set('UTC'); // Start date $date = '2017-01-29'; // End date $end_date = '2017-12-29'; while (strtotime($date) <= strtotime($end_date)) { echo "$date\n"; echo "<br>"; $d = new DateTime( $date ); $d->modify( 'last day of next month' ); $date = $d->format( 'Ymd' ); }
Now this may not be exactly what interests you, because you can try different start dates and not reach the end of the next month, or you may want to use 2/28 for the February date, but then return to the 29th for each subsequent month . But that should come close to the logic you need. I think using DateTime will be part of your answer.
davidethell
source share