Calculate business days from date excluding holidays - PHP

I need to add business days to the specific date, to get the final due date. I would like to exclude weekends, but also national days and public holidays. How can I do this calculation in PHP ? I prefer the simple solution.
0
give a positive ratinggive a negative rating
Hi,

To calculate the due date by using specific date and business days, you can use the function below. Date format used in relation to the function is YYYY-MM-DD. Holidays and national days have to be added into $holidays array.



function calculateDateUsingBusinessDays($startDate,$businessDays) {

$dates = array();
$holidays = array("2020-12-24","2020-12-25","2020-12-26");

for ($v=0;$v<=ceil(($businessDays*1.5)+3+count($holidays));$v++) {
$date = date("Y-m-d", strtotime($startDate) + 86400 * $v);
$weekday = date("w", strtotime($date));
if ( $weekday != 6 AND $weekday != 0 AND !in_array($date, $holidays) ) $dates[] = $date;
}

if( ( date("w", strtotime($startDate)) == 6 OR date("w", strtotime($startDate)) == 0 OR in_array($startDate,$holidays) ) AND $businessDays > 0 ) $businessDays -= 1;

return $dates[$businessDays];
}

echo calculateDateUsingBusinessDays("2020-07-17",5);



When you request to calculate 0 business days from a business day, you will get the same date. If you request to calculate 1 business day from a business day, you will get the date of the nearest business day.

In case you request to calculate 0 or 1 business day from a non-business day, for example from Saturday, you will get the date of the nearest business day. This you can modify according to your needs.
Share on FacebookShare on TwitterShare on LinkedInSend email
1 answer
x
x
2024 AnswerTabsTermsContact us