I want to calculate the number of working days.
By using carbon I can calculate days and reduce weekends.
$num_days = $to_date->diffInWeekdays($from_date) + 1;
And I have an array of holidays, and I want to reduce the number of days if there is a holiday in between the days.
Is there any way to do this.
Thank you
You could use diffInDaysFiltered
to achieve what you're after.
Assuming your holidays are an array of Carbon
instances you could do something like:
$start = Carbon::now()->setDate(2014, 1, 1);
$end = Carbon::now()->setDate(2015, 1, 1);
$holidays = [
Carbon::create(2014, 2, 2),
Carbon::create(2014, 4, 17),
Carbon::create(2014, 5, 19),
Carbon::create(2014, 7, 3),
];
$days = $start->diffInDaysFiltered(function (Carbon $date) use ($holidays) {
return $date->isWeekday() && !in_array($date, $holidays);
}, $end);
If it's just an array of strings then you could do something like:
!in_array($date->format('[the-format-of-your-dates]'), $holidays)
Hope this helps!