Skip to content

Instantly share code, notes, and snippets.

@KaiCMueller
Created April 6, 2020 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KaiCMueller/2dcc548d39f638852731d148a16ce653 to your computer and use it in GitHub Desktop.
Save KaiCMueller/2dcc548d39f638852731d148a16ce653 to your computer and use it in GitHub Desktop.
Calculation of next working day date
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