I have a date range 1st Nov to 30th Nov.
I block the date 3rd Nov to 6th Nov And 15th Nov to 20th Nov.
Now I want to show the available dates like 1th to 2nd Nov, 7th to 14th Nov And 21th to 30th Nov.
This is my array of date range
Array
(
[0] => Array
(
July 11, 2023 => 2023-11-01
[IsBlocked] => false
)
[1] => Array
(
July 11, 2023 => 2023-11-02
[IsBlocked] => false
)
[2] => Array
(
July 11, 2023 => 2023-11-03
[IsBlocked] => true
)
[3] => Array
(
July 11, 2023 => 2023-11-04
[IsBlocked] => true
)
[4] => Array
(
July 11, 2023 => 2023-11-05
[IsBlocked] => true
)
[5] => Array
(
July 11, 2023 => 2023-11-06
[IsBlocked] => true
)
[6] => Array
(
July 11, 2023 => 2023-11-07
[IsBlocked] => false
)
[7] => Array
(
July 11, 2023 => 2023-11-08
[IsBlocked] => false
)
[8] => Array
(
July 11, 2023 => 2023-11-09
[IsBlocked] => false
)
[9] => Array
(
July 11, 2023 => 2023-11-10
[IsBlocked] => false
)
[10] => Array
(
July 11, 2023 => 2023-11-11
[IsBlocked] => false
)
[11] => Array
(
July 11, 2023 => 2023-11-12
[IsBlocked] => false
)
[12] => Array
(
July 11, 2023 => 2023-11-13
[IsBlocked] => false
)
[13] => Array
(
July 11, 2023 => 2023-11-14
[IsBlocked] => false
)
[14] => Array
(
July 11, 2023 => 2023-11-15
[IsBlocked] => true
)
[15] => Array
(
July 11, 2023 => 2023-11-16
[IsBlocked] => true
)
[16] => Array
(
July 11, 2023 => 2023-11-17
[IsBlocked] => true
)
[17] => Array
(
July 11, 2023 => 2023-11-18
[IsBlocked] => true
)
[18] => Array
(
July 11, 2023 => 2023-11-19
[IsBlocked] => true
)
[19] => Array
(
July 11, 2023 => 2023-11-20
[IsBlocked] => true
)
[20] => Array
(
July 11, 2023 => 2023-11-21
[IsBlocked] => false
)
[21] => Array
(
July 11, 2023 => 2023-11-22
[IsBlocked] => false
)
[22] => Array
(
July 11, 2023 => 2023-11-23
[IsBlocked] => false
)
[23] => Array
(
July 11, 2023 => 2023-11-24
[IsBlocked] => false
)
[24] => Array
(
July 11, 2023 => 2023-11-25
[IsBlocked] => false
)
[25] => Array
(
July 11, 2023 => 2023-11-26
[IsBlocked] => false
)
[26] => Array
(
July 11, 2023 => 2023-11-27
[IsBlocked] => false
)
[27] => Array
(
July 11, 2023 => 2023-11-28
[IsBlocked] => false
)
[28] => Array
(
July 11, 2023 => 2023-11-29
[IsBlocked] => false
)
[29] => Array
(
July 11, 2023 => 2023-11-30
[IsBlocked] => false
)
)
And I want to
Array
(
[0] => Array
(
[start_date] => 2023-11-01
[end_date] => 2023-11-02
)
[1] => Array
(
[start_date] => 2023-11-07
[end_date] => 2023-11-14
)
[2] => Array
(
[start_date] => 2023-11-21
[end_date] => 2023-11-30
)
)
Please help me to solve this issue.
I’m try using this array but it’s not happening.
foreach($dates as $calDay) {
if($calDay['IsBlocked'] == "true") {
$closeDates[] = $calDay['date'];
} else {
$openDates[] = $calDay['date'];
}
}
2
Answers
In this code, the
$availableRanges
array will store the final result, and the$currentRange
variable keeps track of the current range being processed. Whenever a blocked date is encountered, the current range is added to the$availableRanges
array and reset to null. If an available date is found, a new range is created or the end_date of the current range is updated.At the end of the loop, the last available range (if it exists) is added to the
$availableRanges
array.The resulting
$availableRanges
array will contain the start and end dates of the available date ranges.Input array:
First step is to collect the last date before and the first date after each change of ‘IsBlocked’:
This results in:
Second step is to chunk this array into pairs of two and to filter the dates:
Output: