<?php
\date_default_timezone_set('UTC');
$dates = ['2019-01-18', '2019-01-01', '2018-12-01', '2018-12-20', null];
$today = new \DateTimeImmutable('2019-01-01');
$yesterday = $today->modify('-1 day');
$results = [];
foreach ($dates as $iDate) {
$_GET['importDate'] = $iDate;
$results[$iDate] = '';
if (!$start = \date_create(!empty($_GET['importDate']) ? $_GET['importDate'] : $yesterday->format('Y-m-d'))) {
die('Invalid Date Supplied');
}
if ($start > $today) {
$results[$iDate] .= 'Dates set in the future are not permitted' . \PHP_EOL;
continue;
}
//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') === $today->format('Y-m')) {
$end = \date_create($yesterday->format('Y-m-d'))->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');
$results[$iDate] .= $dateImport . \PHP_EOL;
//...
}
}
//display the results
$values = array_reduce(\array_keys($results), function($carry, $item) use ($results) {
if (!in_array($results[$item], $carry)) {
$carry[] = $results[$item];
}
return $carry;
}, []);
echo 'Current Date: ' . $today->format('Y-m-d') . \PHP_EOL . \PHP_EOL;
foreach ($values as $info) {
$dates = '"' . implode('", "', array_keys($results, $info)) . '"';
echo <<<EOL
Import Dates: $dates
$info
EOL;
}
preferences:
27.98 ms | 410 KiB | 5 Q