PHP: List Months Between 2 Dates
Based on code:
function list_months($start, $end) { $period = new DatePeriod( (new DateTime($start))->modify('first day of this month'), DateInterval::createFromDateString('1 month'), (new DateTime($end))->modify('first day of next month') ); $month = []; foreach ($period as $dt) { $month[] = $dt->format('Ym'); } return $month; }
Notes:
- Line 3: get the first of the month from start date.
- Line 5: get the first of the end date’s next month. This to include month of end date in result.