﻿ Calculate business days from date excluding holidays - PHP | AnswerTabs

# 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.
 16 Jul 2020 at 04:49 PM
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.

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

\$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;

}

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.
 19 Jul 2020 at 04:23 PM
Related questions