Skip to content

Instantly share code, notes, and snippets.

@z2z
Created July 2, 2020 13:46
Show Gist options
  • Save z2z/f35a5750063c92c2ea76b2011f7de526 to your computer and use it in GitHub Desktop.
Save z2z/f35a5750063c92c2ea76b2011f7de526 to your computer and use it in GitHub Desktop.
business date add exclude holiday php
<?php
# $date must be in YYYY-MM-DD format
# You can pass in either an array of holidays in YYYY-MM-DD format
function addBusinessDays($date,$numDays=1,$holidays='') {
$addDay = 0;
while ($numDays--) {
while (true) {
$addDay++;
$newDate = date('Y-m-d', strtotime("$date +$addDay Days"));
$newDayOfWeek = date('w', strtotime($newDate));
if ( $newDayOfWeek>0 && $newDayOfWeek<6 && !in_array($newDate,$holidays)) break;
}
}
return $newDate;
}
echo addBusinessDays('2020-07-02',20,['2020-07-03','2020-07-06']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment