I am trying to get a Date range for all workdays this week. I have written the following code to do so.
Code
$begin = new DateTime('monday this week'); 2016-07-04
$end = clone $begin;
$end->modify('next friday'); // 2016-07-08
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval, $end);
foreach($daterange as $date) {
echo $date->format('Y-m-d')."<br />";
}
Output
- 2016-07-04
- 2016-07-05
- 2016-07-06
- 2016-07-07
In the output friday is missing. I can fix this by doing $end->modify('next saturday')
but I was wondering why the last day of a DatePeriod
is not included in the range.