Skip to content

Instantly share code, notes, and snippets.

@dcblogdev
Last active December 15, 2015 10:49
Show Gist options
  • Save dcblogdev/5248025 to your computer and use it in GitHub Desktop.
Save dcblogdev/5248025 to your computer and use it in GitHub Desktop.
The snippet below will return the number of week days between 2 dates also exclude any dates in an array such as bank holidays
<?php
function get_working_days($startDate,$endDate){
$holidays = array(
'2013-03-29',
'2013-04-01',
'2013-05-06',
'2013-05-27',
'2013-08-26',
'2013-12-25',
'2013-12-26',
'2014-01-01',
'2014-04-18',
'2014-04-21',
'2014-05-05',
'2014-05-26',
'2014-08-25',
'2014-12-25',
'2014-12-26',
'2015-01-01',
'2015-04-03',
'2015-04-06',
'2015-05-04',
'2015-05-25',
'2015-08-31',
'2015-12-25',
'2015-12-28',
'2016-01-01',
'2016-03-25',
'2016-03-28',
'2016-05-02',
'2016-05-30',
'2016-08-29',
'2016-12-26',
'2016-12-27',
'2017-01-02',
'2017-04-14',
'2017-04-17',
'2017-05-01',
'2017-05-29',
'2017-08-28',
'2017-12-25',
'2017-12-26',
'2018-01-01',
'2018-03-30',
'2018-04-02',
'2018-05-07',
'2018-05-28',
'2018-08-27',
'2018-12-25',
'2018-12-26',
'2019-01-01',
'2019-04-19',
'2019-04-22',
'2019-05-06',
'2019-05-27',
'2019-08-26',
'2019-12-25',
'2019-12-26',
'2020-01-01',
'2020-04-10',
'2020-04-13',
'2020-05-04',
'2020-05-25',
'2020-08-31',
'2020-12-25',
'2020-12-28'
);
$work = 0;
$nowork = 0;
$dayx = strtotime($startDate);
$endx = strtotime($endDate);
while($dayx <= $endx){
$day = date('N',$dayx);
$date = date('Y-m-d',$dayx);
if($day > 5 || in_array($date,$holidays)){
$nowork++;
} else $work++;
$dayx = strtotime($date.' +1 day');
}
return $work;
}
//usage example
echo get_working_days('2013-03-29','2013-04-15');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment