@ 2015-11-01T13:46:52Z <?php
/**
* @param string $start
* @param string $end
*
* @return string[]
*/
function date_period_grid($start, $end)
{
$start = new DateTime($start);
$end = new DateTime($end);
$i = $end->diff($start);
$days = $i->days;
$months = $i->y * 12 + $i->m + ($i->d ? 1 : 0);
$years = $i->y + (($i->m || $i->d) ? 1 : 0);
if ($years > 1) {
$period = new DatePeriod($start, new DateInterval('P1Y'), $years);
} elseif ($months > 1) {
$period = new DatePeriod($start, new DateInterval('P1M'), $months);
} else {
$period = new DatePeriod($start, new DateInterval('P1D'), $days);
}
$result = [];
foreach ($period as $date) {
$result[] = $date->form('Y-m-d');
}
return $result;
}
var_dump(date_period_grid('2012-07-01', '2015-11-01'));
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 7.0.0 - 7.0.33 , 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.6 Fatal error: Uncaught Error: Call to undefined method DateTime::form() in /in/FjJ4O:30
Stack trace:
#0 /in/FjJ4O(36): date_period_grid(Object(DateTime), Object(DateTime))
#1 {main}
thrown in /in/FjJ4O on line 30
Process exited with code 255 . Output for 5.4.0 - 5.4.45 , 5.5.0 - 5.5.38 , 5.6.0 - 5.6.40 Fatal error: Call to undefined method DateTime::form() in /in/FjJ4O on line 30
Process exited with code 255 . preferences:dark mode live preview
242.39 ms | 402 KiB | 376 Q