3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $dt_inicio = new DateTime(); $dt_inicio->setTimestamp(mktime(0,0,0, 6,20,2014)); $dt_fim = new DateTime(); $dt_fim->setTimestamp(mktime(0,0,0, 7,19,2015)); $interval = new DateInterval('P1M'); $diffAno = 2015 - 2014; $nmeses = (7 + ($diffAno * 12)) - 6; $period = new DatePeriod($dt_inicio, $interval, $nmeses); var_dump($period); foreach($period as $date){ $meses[] = array( "mes"=>$date->format('m'), "ano"=>$date->format('Y') ); } var_dump($meses);
Output for 7.1.0 - 7.2.0
object(DatePeriod)#4 (6) { ["start"]=> object(DateTime)#5 (3) { ["date"]=> string(26) "2014-06-20 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } ["current"]=> NULL ["end"]=> NULL ["interval"]=> object(DateInterval)#6 (16) { ["y"]=> int(0) ["m"]=> int(1) ["d"]=> int(0) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) } ["recurrences"]=> int(14) ["include_start_date"]=> bool(true) } array(14) { [0]=> array(2) { ["mes"]=> string(2) "06" ["ano"]=> string(4) "2014" } [1]=> array(2) { ["mes"]=> string(2) "07" ["ano"]=> string(4) "2014" } [2]=> array(2) { ["mes"]=> string(2) "08" ["ano"]=> string(4) "2014" } [3]=> array(2) { ["mes"]=> string(2) "09" ["ano"]=> string(4) "2014" } [4]=> array(2) { ["mes"]=> string(2) "10" ["ano"]=> string(4) "2014" } [5]=> array(2) { ["mes"]=> string(2) "11" ["ano"]=> string(4) "2014" } [6]=> array(2) { ["mes"]=> string(2) "12" ["ano"]=> string(4) "2014" } [7]=> array(2) { ["mes"]=> string(2) "01" ["ano"]=> string(4) "2015" } [8]=> array(2) { ["mes"]=> string(2) "02" ["ano"]=> string(4) "2015" } [9]=> array(2) { ["mes"]=> string(2) "03" ["ano"]=> string(4) "2015" } [10]=> array(2) { ["mes"]=> string(2) "04" ["ano"]=> string(4) "2015" } [11]=> array(2) { ["mes"]=> string(2) "05" ["ano"]=> string(4) "2015" } [12]=> array(2) { ["mes"]=> string(2) "06" ["ano"]=> string(4) "2015" } [13]=> array(2) { ["mes"]=> string(2) "07" ["ano"]=> string(4) "2015" } }
Output for 5.4.30 - 7.0.20
object(DatePeriod)#4 (6) { ["start"]=> object(DateTime)#5 (3) { ["date"]=> string(26) "2014-06-20 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } ["current"]=> NULL ["end"]=> NULL ["interval"]=> object(DateInterval)#6 (15) { ["y"]=> int(0) ["m"]=> int(1) ["d"]=> int(0) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) } ["recurrences"]=> int(14) ["include_start_date"]=> bool(true) } array(14) { [0]=> array(2) { ["mes"]=> string(2) "06" ["ano"]=> string(4) "2014" } [1]=> array(2) { ["mes"]=> string(2) "07" ["ano"]=> string(4) "2014" } [2]=> array(2) { ["mes"]=> string(2) "08" ["ano"]=> string(4) "2014" } [3]=> array(2) { ["mes"]=> string(2) "09" ["ano"]=> string(4) "2014" } [4]=> array(2) { ["mes"]=> string(2) "10" ["ano"]=> string(4) "2014" } [5]=> array(2) { ["mes"]=> string(2) "11" ["ano"]=> string(4) "2014" } [6]=> array(2) { ["mes"]=> string(2) "12" ["ano"]=> string(4) "2014" } [7]=> array(2) { ["mes"]=> string(2) "01" ["ano"]=> string(4) "2015" } [8]=> array(2) { ["mes"]=> string(2) "02" ["ano"]=> string(4) "2015" } [9]=> array(2) { ["mes"]=> string(2) "03" ["ano"]=> string(4) "2015" } [10]=> array(2) { ["mes"]=> string(2) "04" ["ano"]=> string(4) "2015" } [11]=> array(2) { ["mes"]=> string(2) "05" ["ano"]=> string(4) "2015" } [12]=> array(2) { ["mes"]=> string(2) "06" ["ano"]=> string(4) "2015" } [13]=> array(2) { ["mes"]=> string(2) "07" ["ano"]=> string(4) "2015" } }
Output for 5.3.27 - 5.3.29, 5.4.17 - 5.4.29
object(DatePeriod)#4 (6) { ["start"]=> object(DateTime)#5 (3) { ["date"]=> string(19) "2014-06-20 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } ["current"]=> NULL ["end"]=> NULL ["interval"]=> object(DateInterval)#6 (15) { ["y"]=> int(0) ["m"]=> int(1) ["d"]=> int(0) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) } ["recurrences"]=> int(14) ["include_start_date"]=> bool(true) } array(14) { [0]=> array(2) { ["mes"]=> string(2) "06" ["ano"]=> string(4) "2014" } [1]=> array(2) { ["mes"]=> string(2) "07" ["ano"]=> string(4) "2014" } [2]=> array(2) { ["mes"]=> string(2) "08" ["ano"]=> string(4) "2014" } [3]=> array(2) { ["mes"]=> string(2) "09" ["ano"]=> string(4) "2014" } [4]=> array(2) { ["mes"]=> string(2) "10" ["ano"]=> string(4) "2014" } [5]=> array(2) { ["mes"]=> string(2) "11" ["ano"]=> string(4) "2014" } [6]=> array(2) { ["mes"]=> string(2) "12" ["ano"]=> string(4) "2014" } [7]=> array(2) { ["mes"]=> string(2) "01" ["ano"]=> string(4) "2015" } [8]=> array(2) { ["mes"]=> string(2) "02" ["ano"]=> string(4) "2015" } [9]=> array(2) { ["mes"]=> string(2) "03" ["ano"]=> string(4) "2015" } [10]=> array(2) { ["mes"]=> string(2) "04" ["ano"]=> string(4) "2015" } [11]=> array(2) { ["mes"]=> string(2) "05" ["ano"]=> string(4) "2015" } [12]=> array(2) { ["mes"]=> string(2) "06" ["ano"]=> string(4) "2015" } [13]=> array(2) { ["mes"]=> string(2) "07" ["ano"]=> string(4) "2015" } }
Output for 5.3.19 - 5.3.26, 5.4.0 - 5.4.16
object(DatePeriod)#4 (0) { } array(14) { [0]=> array(2) { ["mes"]=> string(2) "06" ["ano"]=> string(4) "2014" } [1]=> array(2) { ["mes"]=> string(2) "07" ["ano"]=> string(4) "2014" } [2]=> array(2) { ["mes"]=> string(2) "08" ["ano"]=> string(4) "2014" } [3]=> array(2) { ["mes"]=> string(2) "09" ["ano"]=> string(4) "2014" } [4]=> array(2) { ["mes"]=> string(2) "10" ["ano"]=> string(4) "2014" } [5]=> array(2) { ["mes"]=> string(2) "11" ["ano"]=> string(4) "2014" } [6]=> array(2) { ["mes"]=> string(2) "12" ["ano"]=> string(4) "2014" } [7]=> array(2) { ["mes"]=> string(2) "01" ["ano"]=> string(4) "2015" } [8]=> array(2) { ["mes"]=> string(2) "02" ["ano"]=> string(4) "2015" } [9]=> array(2) { ["mes"]=> string(2) "03" ["ano"]=> string(4) "2015" } [10]=> array(2) { ["mes"]=> string(2) "04" ["ano"]=> string(4) "2015" } [11]=> array(2) { ["mes"]=> string(2) "05" ["ano"]=> string(4) "2015" } [12]=> array(2) { ["mes"]=> string(2) "06" ["ano"]=> string(4) "2015" } [13]=> array(2) { ["mes"]=> string(2) "07" ["ano"]=> string(4) "2015" } }