Created
April 6, 2020 12:53
-
-
Save KaiCMueller/2dcc548d39f638852731d148a16ce653 to your computer and use it in GitHub Desktop.
Calculation of next working day date
This file contains hidden or 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
class WorkingDaysCalculator | |
{ | |
const SUNDAY = 0; | |
const SATURDAY = 6; | |
/** | |
* Get next working day date | |
* | |
* @param \DateTime $baseDate - default now | |
* @param int $additionalWorkingDays - default 0 | |
* | |
* @return \DateTime | |
*/ | |
public function calculateNextWorkingDay(DateTime $baseDate, $additionalWorkingDays = 0): DateTime | |
{ | |
$date = clone $baseDate; | |
if ((int)$date->format('w') === self::SUNDAY) { | |
$date->add(new DateInterval('P1D')); | |
} | |
if ((int)$date->format('w') === self::SATURDAY) { | |
$date->add(new DateInterval('P2D')); | |
} | |
for ($i = 0; $i < $additionalWorkingDays; $i++) { | |
$date->add(new DateInterval('P1D')); | |
if ((int)$date->format('w') === self::SUNDAY) { | |
$date->add(new DateInterval('P1D')); | |
} | |
if ((int)$date->format('w') === self::SATURDAY) { | |
$date->add(new DateInterval('P2D')); | |
} | |
} | |
return $date; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment