Calculating business days can be cumbersome. Here is an iterator for handling business days. Usage examples below for adding # of business days and calculating how many business days between two dates.
Here is the iterator class
https://gist.github.com/styks1987/29dd0f6a68e3b07ba70fec18f732eb86
Usage
Counting # of Business Days between two dates
<?php
function countBusinessDays($start, $stop)
{
if($start > $stop){
$tmpStart = clone $start;
$start = clone $stop;
$stop = clone $tmpStart;
}
$period = new \DatePeriod($start->setTime(0,0,0), new \DateInterval('P1D'), $stop->setTime(23,59,59), \DatePeriod::EXCLUDE_START_DATE);
$periodIterator = new BusinessDayPeriodIterator($period);
$businessDays = 0;
while($periodIterator->valid()){
if(!$periodIterator->isWeekend()){
$businessDays++;
}
$periodIterator->next();
}
return $businessDays;
}
?>
Add # of business days
<?php
function addBusinessDays(\DateTime $startDateTime, $daysToAdd)
{
$endDateTime = clone $startDateTime;
$endDateTime->add(new \DateInterval('P' . $daysToAdd . 'D'))->setTime(23,59,59);
$period = new \DatePeriod($startDateTime, new \DateInterval('P1D'), $endDateTime);
$periodIterator = new BusinessDayPeriodIterator($period);
$adjustedEndingDate = clone $startDateTime;
while($periodIterator->valid()){
$adjustedEndingDate = $periodIterator->current();
if($periodIterator->isWeekend()){
$periodIterator->extend();
}
$periodIterator->next();
}
return $adjustedEndingDate;
}
?>