How can I create a function that can return accurate start and end date within date range of given date that separate by week.
For example, a date range is given,
$startDate = '2022-08-05';
$endDate = '2022-08-16';
(2022-08-01 is the first day of the week but I want the data return start from the date that user given so do the end date)
So the output should be like this:
| 2022-08-05 to 2022-08-07 | 2022-08-08 to 2022-08-14 | 2022-08-15 to 2022-08-16 |
|---|---|---|
| data | data | data |
My problem is function I found cannot get data start from that date that user given but start from the first day of week and I have no idea how to modify it or create a new function. Any help is appreciate.
function rangeWeek ($datestr) {
date_default_timezone_set (date_default_timezone_get());
$dt = strtotime ($datestr);
return array (
"start" => date ('N', $dt) == 1 ? date ('Y-m-d', $dt) : date ('Y-m-d', strtotime ('last monday', $dt)),
"end" => date('N', $dt) == 7 ? date ('Y-m-d', $dt) : date ('Y-m-d', strtotime ('next sunday', $dt))
);
}
If I set the start date to '2022-08-05' then the output will be start from the first day of the week, how to let it start from '2022-08-05'?
print_r (rangeWeek('2022-08-05'));//output = Array ( [start] => 2022-08-01 [end] => 2022-08-07 )