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
(
[date] => 2023-11-01
[IsBlocked] => false
)
[1] => Array
(
[date] => 2023-11-02
[IsBlocked] => false
)
[2] => Array
(
[date] => 2023-11-03
[IsBlocked] => true
)
[3] => Array
(
[date] => 2023-11-04
[IsBlocked] => true
)
[4] => Array
(
[date] => 2023-11-05
[IsBlocked] => true
)
[5] => Array
(
[date] => 2023-11-06
[IsBlocked] => true
)
[6] => Array
(
[date] => 2023-11-07
[IsBlocked] => false
)
[7] => Array
(
[date] => 2023-11-08
[IsBlocked] => false
)
[8] => Array
(
[date] => 2023-11-09
[IsBlocked] => false
)
[9] => Array
(
[date] => 2023-11-10
[IsBlocked] => false
)
[10] => Array
(
[date] => 2023-11-11
[IsBlocked] => false
)
[11] => Array
(
[date] => 2023-11-12
[IsBlocked] => false
)
[12] => Array
(
[date] => 2023-11-13
[IsBlocked] => false
)
[13] => Array
(
[date] => 2023-11-14
[IsBlocked] => false
)
[14] => Array
(
[date] => 2023-11-15
[IsBlocked] => true
)
[15] => Array
(
[date] => 2023-11-16
[IsBlocked] => true
)
[16] => Array
(
[date] => 2023-11-17
[IsBlocked] => true
)
[17] => Array
(
[date] => 2023-11-18
[IsBlocked] => true
)
[18] => Array
(
[date] => 2023-11-19
[IsBlocked] => true
)
[19] => Array
(
[date] => 2023-11-20
[IsBlocked] => true
)
[20] => Array
(
[date] => 2023-11-21
[IsBlocked] => false
)
[21] => Array
(
[date] => 2023-11-22
[IsBlocked] => false
)
[22] => Array
(
[date] => 2023-11-23
[IsBlocked] => false
)
[23] => Array
(
[date] => 2023-11-24
[IsBlocked] => false
)
[24] => Array
(
[date] => 2023-11-25
[IsBlocked] => false
)
[25] => Array
(
[date] => 2023-11-26
[IsBlocked] => false
)
[26] => Array
(
[date] => 2023-11-27
[IsBlocked] => false
)
[27] => Array
(
[date] => 2023-11-28
[IsBlocked] => false
)
[28] => Array
(
[date] => 2023-11-29
[IsBlocked] => false
)
[29] => Array
(
[date] => 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'];
}
}
>Solution :
$dates = [
// ... your existing array of dates ...
];
$availableRanges = [];
$currentRange = null;
foreach ($dates as $calDay) {
if ($calDay['IsBlocked'] == "true") {
// If the current range exists, add it to the availableRanges array
if ($currentRange) {
$availableRanges[] = $currentRange;
$currentRange = null;
}
} else {
// If the current range does not exist, create a new range
if (!$currentRange) {
$currentRange = [
'start_date' => $calDay['date'],
'end_date' => $calDay['date']
];
}
// Update the end_date of the current range
$currentRange['end_date'] = $calDay['date'];
}
}
// Add the last available range if it exists
if ($currentRange) {
$availableRanges[] = $currentRange;
}
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.