3v4l.org

run code in 300+ PHP versions simultaneously
<?php \date_default_timezone_set('UTC'); $dates = ['2019-01-18', '2018-12-20', '2019-01-01', null]; echo 'Current Date: ' . date('Y-m-d') . \PHP_EOL; foreach ($dates as $iDate) { $_GET['importDate'] = $iDate; echo 'Import Date of: "' . $iDate . '"' . \PHP_EOL . \PHP_EOL; if (!$start = \date_create(!empty($_GET['importDate']) ? $_GET['importDate'] : 'yesterday')) { die('Invalid Date Supplied'); } //start at the first day of the month $start->modify('first day of this month')->setTime(0,0,0); //validate the current month to prevent future dates if ($start->format('Y-m') === date('Y-m')) { $end = \date_create('yesterday')->setTime(23,59,59); if ($end < $start) { //yesterday was last month $end = clone $start; } } else { $end = clone $start; $end->modify('last day of this month'); } //end at midnight $end->setTime(23,59,59); $importDates = new \DatePeriod($start, new \DateInterval('P1D'), $end); foreach ($importDates as $date) { $dateImport = $date->format('Y-m-d'); echo $dateImport . \PHP_EOL; //... } echo '------' . \PHP_EOL; }

preferences:
34.53 ms | 406 KiB | 5 Q