php - Custom Range of dates

I have an array of dates which look like this, it's a list of dates.
Array
(
[0] => 2021-07-05
[1] => 2021-07-06
[2] => 2021-07-07
[3] => 2021-07-08
[4] => 2021-07-09
[5] => 2021-07-10
[6] => 2021-07-11
[7] => 2021-07-12
[8] => 2021-07-13
[9] => 2021-07-14
[10] => 2021-07-15
[11] => 2021-07-16
[12] => 2021-07-17
[13] => 2021-07-18
[14] => 2021-07-19
[15] => 2021-07-20
[16] => 2021-07-21
[17] => 2021-07-22
)
How can i get the below date ranges? Every 6 dates, because i need to loop after in a specific way.
Array
(
[0] => Array
(
[from] => 2021-07-05
[to] => 2021-07-11
)
[1] => Array
(
[from] => 2021-07-11
[to] => 2021-07-17
)
[2] => Array
(
[from] => 2021-07-17
[to] => 2021-07-22
)
)
Answer
Solution:
You can try this example.
$from = $dates[0];
$to = $from;
$result = [];
foreach($dates as $date) {
if (strtotime($from) + 60 * 60 * 24 * 6 < strtotime($date)) {
$result[] = ['from' => $from, 'to' => $to];
$from = $to;
}
$to = $date;
}
$result[] = ['from' => $from, 'to' => $to];
print_r($result);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: cannot set properties of undefined (setting '_dt_cellindex')
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.