Get Working Days between 2 dates with PHP

We needed to work out the working days between two dates for a small calendar application we had to write. After looking round the net for a few minuets we decided to write a quick function for ourself.

It takes 3 inputs. A From Date (Epoch) , a To Date (Epoch) and an array with Bank Holiday dates in it. By the time it has finished it has a variable $WorkingDays with the number of working days in the give range.

The main logic goes like this

while ( $StartTime <= $EndTime ) {
if (
date(”D”,$StartTime) == “Sat” ||
date(”D”,$StartTime) == “Sun” ||
array_key_exists(date(”Ymd”,$StartTime),$BankHolidays) ) {

$IgnorDays++;

} else {
$WorkingDays++;

}

$StartTime=$StartTime+86400;
}

Leave a Reply