Pick up and list months between dates of different years

1

I have the following question. Initial Date: 01/10/2017 and final date 01/10/2018. How can I list the months between these dates?

For example: October (17), November (17), December (17), January (18) ... and so on until September (18).

    
asked by anonymous 20.12.2017 / 13:40

1 answer

2

Here's an example:

// Data de ínicio
$start    = (new DateTime('2017-10-01'))->modify('first day of this month');
// Data final
$end      = (new DateTime('2018-10-01'))->modify('first day of next month');
// Define qual será o intervalo a ser calculado
$interval = DateInterval::createFromDateString('1 month');
// Cria o período de data entre o inicio, final e o intervalo
$period   = new DatePeriod($start, $interval, $end);

// conforme o intervalo, retorna todas datas
foreach ($period as $dt) {
   echo $dt->format("Y-m") . "<br>\n";
}

Test online: link

    
20.12.2017 / 13:46