show all months and years in select options from a specific date to current date in php

Show all Months and Years in select options from a specific date to current date in PHP

Here the PHP code is given to show all months and years in select option from a specific date to current date.

<select>
<option>Choose Month</option>
<?php

$start = new DateTime(‘2017-Aug-01’);
$start->modify(‘first day of previous month’);
$end = new DateTime(date(‘Y/m/d H:i:s’));
$end->modify(‘last day of this month’);
$interval = DateInterval::createFromDateString(‘1 month’);
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo “<option value=”. $dt->format(“Y-M”) .”>” . $dt->format(“Y-M”) . “</option>”;
}
?>
</select>