I try to get all days within a period of time. It does not matter if it is a full day or only some hours. My current code using DatePeriod does not work for me.
Example:
[start] => DateTime Object
(
[date] => 2014-01-27 22:40:40
[timezone_type] => 1
[timezone] => +00:00
)
[end] => DateTime Object
(
[date] => 2014-01-28 17:40:40
[timezone_type] => 1
[timezone] => +00:00
)
$interval = new DateInterval('P1D'); // 1Day Interval
$daterange = new DatePeriod($time['start'], $interval ,$time['end']);
$return['days'] = array();
foreach($daterange as $date){
$return['days'][] = $date->format("Y-m-d");
}
I would like to get
[0] => 2014-01-27
[1] => 2014-01-28
But i only get the
[0] => 2014-01-27
Is it possible to change DatePeriod parameters or something ?
DatePeriod object ignores last period. You can solve that by increasing end date by your interval:
$interval = new DateInterval('P1D');
$start->setTime(0, 0);
$end->setTime(0, 0)->add($interval);
$periods = new DatePeriod($start, $interval, $end);
demo
Updated: remove (set to 00:00:00) time parts on start and end dates, if you are only looping via days.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With