php date interval/date period not behaving as expected
-
13-11-2019 - |
Question
After doing some research into Dateperiod, it turns out it by default excludes the enddate, even though it doesn't explicitly state that anywhere in the PHP manual. I also didn't notice any option for including it. The only option there seems to be is the option to exclude the start date. Has anyone else run across this?
Solution
you forgot the time
$start_date = '02/28/2012 00:00:00';
$end_date = '02/29/2012 23:59:59';
$intrDate = '1D';
$start = new \DateTime($start_date);
$end = new \DateTime($end_date);
$interval = new \DateInterval('P'.$intrDate);
$period = new \DatePeriod($start, $interval, $end);
print_r($start_date);
print_r($end_date);
print_r($period);
foreach ($period as $day) {
$dates[] = array(
'eventID' => $event_id,
'date' => $day->format('Y-m-d'),
'max' => $data['numAttending']);
}
print_r($dates);
exit;
this output:
Array
(
[0] => Array
(
[eventID] =>
[date] => 2012-02-28
[max] =>
)
[1] => Array
(
[eventID] =>
[date] => 2012-02-29
[max] =>
)
)
without the time, you will get:
Array
(
[0] => Array
(
[eventID] =>
[date] => 2012-02-28
[max] =>
)
)
OTHER TIPS
L - Whether it's a leap year
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow