Date: 03/09/06 (Algorithms) Keywords: php I'm working on a major enterprise scheduling/asset management/kitchen sink application and recently ran into the common calculate x number of business days from given date problem. I had trouble finding a simple,concise algorithm to handle it on the net(i found plenty of ugly ones) so i decided to share the one I came up with - which is clean and seems to work nicely - with you guys. Any comments/improvements are much appreciated. This example is implemented in php. $offset is the number of business days ahead to calculate. $date is unixtime start date.
function increment_business_days($offset,$date){
if($offset > 0){//if the offset is greater than 0
$dayofweek = date('D',$date);
if($dayofweek == 'Fri'){//if today is friday
$date += 259200;//add 3 days worth of seconds to push us to Monday
}else{
$date += 86400; //else add 1 days worth of seconds
}
$offset--;//decrement the offset
return $this->increment_business_days($offset,$date);//call me recursively
}else{//else we have arrived at our end date, get outta here
return $date;
}
}
$date += 3600;//add an hour to compensate for daylight savings weirdness.
Source: http://community.livejournal.com/algorithms/74067.html
|