<?php
function getYearsAndMonthsBetweenDates($startDate, $endDate) {
$start = new DateTime($startDate);
$end = new DateTime($endDate);
$interval = new DateInterval('P1M');
$periods = new DatePeriod($start, $interval, $end); // We add $interval to include the end month too
$yearMonths = [];
foreach ($periods as $period) {
$yearMonths[] = $period->format('Y-m');
}
return $yearMonths;
}
$startDate = "2019-01-13";
$endDate = "2023-05-02";
$yearsAndMonths = getYearsAndMonthsBetweenDates($startDate, $endDate);
foreach($yearsAndMonths as $yearMonth) {
echo $yearMonth . PHP_EOL;
}
To embed this program on your website, copy the following code and paste it into your website's HTML: