3v4l.org

run code in 300+ PHP versions simultaneously
<?php $start = "2017-01-08"; $stop = "2017-01-20"; $chosenInterval = "week"; function getDates($start, $stop, $chosenInterval = "week") { $startDT = new DateTime($start); $stopDT = new DateTime($stop); if(isset($chosenInterval) && ($chosenInterval == "week" || $chosenInterval == "day") ) { $timeFormat = "F j, Y"; } else { $timeFormat = "F Y"; $chosenInterval = "month"; } $begin = (new DateTime($start))->modify('first day of this month'); $end = (new DateTime($stop))->modify('first day of next month'); $interval = DateInterval::createFromDateString('1 '.$chosenInterval); $period = new DatePeriod($begin, $interval, $end); foreach($period as $dt) { if ($chosenInterval !== "month" && ($dt < $startDT || $dt > $stopDT)) continue; $dataLabels[] = $dt->format($timeFormat); } return $dataLabels; } var_dump(getDates($start, $stop)); var_dump(getDates($start, $stop, "day")); var_dump(getDates($start, $stop, "month"));
Output for 7.0.0 - 7.0.23, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
array(2) { [0]=> string(15) "January 8, 2017" [1]=> string(16) "January 15, 2017" } array(13) { [0]=> string(15) "January 8, 2017" [1]=> string(15) "January 9, 2017" [2]=> string(16) "January 10, 2017" [3]=> string(16) "January 11, 2017" [4]=> string(16) "January 12, 2017" [5]=> string(16) "January 13, 2017" [6]=> string(16) "January 14, 2017" [7]=> string(16) "January 15, 2017" [8]=> string(16) "January 16, 2017" [9]=> string(16) "January 17, 2017" [10]=> string(16) "January 18, 2017" [11]=> string(16) "January 19, 2017" [12]=> string(16) "January 20, 2017" } array(1) { [0]=> string(12) "January 2017" }
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 array(2) { [0]=> string(15) "January 8, 2017" [1]=> string(16) "January 15, 2017" } array(13) { [0]=> string(15) "January 8, 2017" [1]=> string(15) "January 9, 2017" [2]=> string(16) "January 10, 2017" [3]=> string(16) "January 11, 2017" [4]=> string(16) "January 12, 2017" [5]=> string(16) "January 13, 2017" [6]=> string(16) "January 14, 2017" [7]=> string(16) "January 15, 2017" [8]=> string(16) "January 16, 2017" [9]=> string(16) "January 17, 2017" [10]=> string(16) "January 18, 2017" [11]=> string(16) "January 19, 2017" [12]=> string(16) "January 20, 2017" } array(1) { [0]=> string(12) "January 2017" }

preferences:
175.53 ms | 403 KiB | 222 Q