PHP: Calculating Business days from given dates or for particular period.

Whenever i get time i used to answers in PHP related questions in Stackoverflow. Today i come across common question being ask by number of developers, is to get the "Bussiness days" in a specific period. Developer write unneccessary of lines of codes, which we can get easily by using Datetime functions available with PHP.

PHP has numbers of datetime related functions which we can use to get Business day between two dates. Even if we have list of holidays we can also consider that for calculating.

Here is a simple example how you will get Business day in a particular period:

Let say i have start date as "2013-04-01" and end date as "2013-04-30" and need all Business day (which means excluding Saturday and Sunday) for this period. Additionaly i have a list of holiday list for this month.

Here i am using DateTime and DatePeriod functions of PHP.
$startDate = new DateTime( '2013-04-01' );  //intialize start date
$endDate = new DateTime( '2013-04-30' );  //initialize end date
$holiday = array('2013-04-11','2013-04-25');  //this is assumed list of holiday
$interval = new DateInterval('P1D');  // set the interval as 1 day
$daterange = new DatePeriod($startDate, $interval ,$endDate);
foreach($daterange as $date){
if($date->format("N") <6 AND !in_array($date->format("Y-m-d"),$holiday))
$result[] = $date->format("Y-m-d");
}
print_r($result);
//output
Array
(
    [0] => 2013-04-01
    [1] => 2013-04-02
    [2] => 2013-04-03
    [3] => 2013-04-04
    [4] => 2013-04-05
    [5] => 2013-04-08
    [6] => 2013-04-09
    [7] => 2013-04-10
    [8] => 2013-04-12
    [9] => 2013-04-15
    [10] => 2013-04-16
    [11] => 2013-04-17
    [12] => 2013-04-18
    [13] => 2013-04-19
    [14] => 2013-04-22
    [15] => 2013-04-23
    [16] => 2013-04-24
    [17] => 2013-04-26
    [18] => 2013-04-29
    [19] => 2013-04-30
)



Your feedbacks are most welcome..