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"));

preferences:
16.45 ms | 402 KiB | 5 Q