Last active
July 22, 2020 20:26
-
-
Save cesurapp/84e7d5d3392b2b6dcd3ae101f89b3f00 to your computer and use it in GitHub Desktop.
Add Working Day with Holidays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add Working Day with Holidays | |
* | |
* @param \DateTimeInterface $first | |
* @param int|null $weekDay | |
* @param \DateTimeInterface[]|null $holidays | |
* | |
* @return \DateTimeInterface | |
*/ | |
public function addWorkingDay(\DateTimeInterface $first, ?int $weekDay, ?array $holidays): \DateTimeInterface | |
{ | |
// Add Week Day | |
$last = $weekDay ? (clone $first)->modify("+{$weekDay} weekday") : $first; | |
// Append Holiday | |
$append = 0; | |
foreach ($holidays as $index => $holiday) { | |
// Disable Week Day | |
$w = (int)$holiday->format('w'); | |
if ($w === 0 || $w === 6) { | |
continue; | |
} | |
if (($holiday >= $first) && ($holiday <= $last)) { | |
unset($holidays[$index]); | |
$append++; | |
} | |
} | |
if ($append) { | |
return addWeekDay($last, $append, $holidays); | |
} | |
return $last; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example