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