3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $start = new DateTime('2012-01-01'); $start->modify('first day of this month'); $end = new DateTime(); $end->modify('last day of next month'); $interval = DateInterval::createFromDateString('5 month'); $period = new DatePeriod($start, $interval, $end); foreach ($period as $dt) { // echo $dt->format("Y-m-d") . PHP_EOL; $date[$dt->format("Y")][] = $dt->format("Y-m-d"); } print_r($date);
based on V9vjj
Output for 5.3.18 - 5.4.45, 5.6.28, 7.0.20 - 7.2.0
Array ( [2012] => Array ( [0] => 2012-01-01 [1] => 2012-06-01 [2] => 2012-11-01 ) [2013] => Array ( [0] => 2013-04-01 [1] => 2013-09-01 ) [2014] => Array ( [0] => 2014-02-01 [1] => 2014-07-01 [2] => 2014-12-01 ) [2015] => Array ( [0] => 2015-05-01 [1] => 2015-10-01 ) )
Output for 5.5.24 - 5.6.21, 7.0.0 - 7.0.6
Array ( [2012] => Array ( [0] => 2012-01-01 [1] => 2012-06-01 [2] => 2012-11-01 ) [2013] => Array ( [0] => 2013-04-01 [1] => 2013-09-01 ) [2014] => Array ( [0] => 2014-02-01 [1] => 2014-07-01 [2] => 2014-12-01 ) [2015] => Array ( [0] => 2015-05-01 [1] => 2015-10-01 ) [2016] => Array ( [0] => 2016-03-01 ) )