Page 1 of 1

Adding Business Days to Date and Excluding Holidays

Posted: Sun Dec 20, 2009 7:59 pm
by tgk
I am trying to write a script to calculate the business day and date from a given start date. For example, if I have $startdate = '2009-12-21', I need code to calculate 7 Business days and exclude 2009-12-25 due to Christmas being a Holiday. Business Days can be assumed to be Monday through Friday.

I found the following code on another forum, but not sure how reliable it really is as it is not working correctly. It keeps showing the current date when I set $businessdays = 1 and shows 2009-12-25 when I set $businessdays = 4

Does anyone know what is wrong?

Code: Select all

 
function add_business_days($startdate,$businessdays,$holidays,$dateformat){  
   $i=1;  
   $dayx = strtotime($startdate);  
 
  while($i < $businessdays){   
     $day = date('N',$dayx);   
     $date = date('Y-m-d',$dayx);   
     if($day < 6 && !in_array($date,$holidays))$i++;   
     $dayx = strtotime($date.' +1 day');  
  }  
return date($dateformat,$dayx); 
} 
 
//Example date_default_timezone_set('Europe\London'); 
$startdate = '2009-12-21'; 
$holidays=array("2009-12-25"); 
echo '<p>Start date: '.date('r',strtotime( $startdate)); 
echo '<p>'.add_business_days($startdate,1,$holidays,'r'); // Should show 2009-12-22
echo '<p>'.add_business_days($startdate,4,$holidays,'r'); // Should show 2009-12-26
 

Re: Adding Business Days to Date and Excluding Holidays

Posted: Sun Dec 20, 2009 8:36 pm
by daedalus__
tgk wrote:I am trying to write a script to calculate the business day and date
i understand that
tgk wrote: from a given start date.
but not that.

but heres the dilly anyway.

you can use the php date function to get the current date and the day of the week and whatever. its the date function.

http://php.net/manual/en/function.date. ... parameters

so what you can do is make a enumeration or array or whatever in php. in fact i saw an enumeration class for php (cool).

anwyays, make a list of all the holidays in your country with their unix timestamps for whatever year it is in this enumeration and then use the enumeration to exclude holidays from the list.

or something.