How to get only holiday date from date array
I'm trying to make a holiday array, it is based on work_day.
For example is if in 1 group there's only day 1-2 (sunday-monday), then the other day (tuesday-saturday) is holiday. And based on that days, I want to get a holiday date array from start of the year until now.
I have a table like this:
attendance_group_id | day
GROUP-001 | 1
GROUP-001 | 2
GROUP-002 | 1
GROUP-003 | 1
What I've done first is getting all data from work_day table using code bellow:
foreach ($group_work_hour as $hours) {
foreach ($hours as $hour){
$work_day = (object) array();
$work_day->group_id = $hour->attendance_group_id;
$work_day->day = $hour->day;
$work_day_arr[] = $work_day;
}
}
And the result from code above is like bellow, every group can have day up to 7 day (1-7). 1 for sunday and 7 for saturday.
Array work day
array (
0 =>
(object) array(
'group_id' => '854b5b57-f863-4e48-ba9b-617899c64750',
'day' => 2,
),
1 =>
(object) array(
'group_id' => '854b5b57-f863-4e48-ba9b-617899c64750',
'day' => 3,
),
2 =>
(object) array(
'group_id' => '854b5b57-f863-4e48-ba9b-617899c64750',
'day' => 4,
),
3 =>
(object) array(
'group_id' => '854b5b57-f863-4e48-ba9b-617899c64750',
'day' => 5,
),
4 =>
(object) array(
'group_id' => '854b5b57-f863-4e48-ba9b-617899c64750',
'day' => 6,
),
5 =>
(object) array(
'group_id' => '854b5b57-f863-4e48-ba9b-617899c64750',
'day' => 7,
),
6 =>
(object) array(
'group_id' => 'f3f739d2-77fe-4e1f-b7fc-44242f52610b',
'day' => 2,
),
7 =>
(object) array(
'group_id' => 'f3f739d2-77fe-4e1f-b7fc-44242f52610b',
'day' => 3,
),
8 =>
(object) array(
'group_id' => 'f3f739d2-77fe-4e1f-b7fc-44242f52610b',
'day' => 4,
),
9 =>
(object) array(
'group_id' => 'f3f739d2-77fe-4e1f-b7fc-44242f52610b',
'day' => 5,
),
10 =>
(object) array(
'group_id' => 'f3f739d2-77fe-4e1f-b7fc-44242f52610b',
'day' => 6,
),
And what I've done next is to get every date from start of the year until today and store it to an array.
$now = \Carbon\Carbon::now()->format('Y-m-d');
$start = \Carbon\Carbon::parse($now)->startOfYear()->format('Y-m-d');
$dateRangePeriod = \Carbon\CarbonPeriod::create($start, $now);
$dateRange = [];
foreach ($dateRangePeriod as $key => $date) {
$dateRange[] = $date->format('Y-m-d');
}
The result from code above is like this:
Array date
array (
0 => '2022-04-01',
1 => '2022-04-02',
2 => '2022-04-03',
3 => '2022-04-04',
4 => '2022-04-05',
5 => '2022-04-06',
6 => '2022-04-07',
7 => '2022-04-08',
8 => '2022-04-09',
9 => '2022-04-10',
10 => '2022-04-11',
11 => '2022-04-12',
12 => '2022-04-13',
)
What I want to do next is, I want to get only holiday date from array date above. But I don't know how to do that because from work_day I only get group_id and the number of day.
How to know if the day from array date is the holiday based on array work_day.
I'm trying some way to doing it, but I can't found the solution yet. I've tried like code bellow, but still not working:
// Loop all date from array date
foreach ($dateRange as $range) {
// Get number of day from date
$day = date('N', strtotime($range));
// Loop work day array
foreach ($work_day_arr as $work) {
// When day from work not equals to day from date (this is a right way to check if days from array date is a holiday)?
if($work->day != $day) {
// Check if group_id & off_date (holiday date) is not set
if(!isset($data['attendance_group_id'], $data['off_date'])) {
$data['attendance_group_id'] = $work->group_id;
$data['off_date'] = date('Y-m-d', strtotime($range));
$objData[] = $data;
}
}
}
}
The example result that I want is something like this (excluding holiday date):
From:
array (
0 => '2022-04-01', // holiday date
1 => '2022-04-02',
2 => '2022-04-03',
3 => '2022-04-04',
4 => '2022-04-05',
5 => '2022-04-06', // holiday date
6 => '2022-04-07',
7 => '2022-04-08',
8 => '2022-04-09',
9 => '2022-04-10',
10 => '2022-04-11',
11 => '2022-04-12', // holiday date
12 => '2022-04-13', // holiday date
)
To (excluding holiday date):
array (
0 => '2022-04-02',
1 => '2022-04-03',
2 => '2022-04-04',
3 => '2022-04-05',
4 => '2022-04-07',
5 => '2022-04-08',
6 => '2022-04-09',
7 => '2022-04-10',
8 => '2022-04-11',
)
Please or to participate in this conversation.