Re: Calendar math .... how to count in week days....?
| From: | Anthony | Date: | Fri, 14 May 2004 17:43:06 +0000 |
| Subject: | Re: Calendar math .... how to count in week days....? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-186243@lists.php.net to get a copy of this message | ||
The PEAR functions didn't seem to do what I was looking for. I built this
function based on an idea given to me by Daryl Meese.
After much trial and error, it actualy does what I need. My function is
bassed on hours but you can easily change that. It will add or subtract
days.
- Anthony
/* buisinessDays
* Calculates a new date bassed on buisiness hours
* vars: hours to adjust by, timestamp
* returns: adjusted timestamp
*/
function businessDays($hours,$date) {
// this function assumes $date is a business day
$dayHours=7.5; // set how many hours are in a day
// set the days we need to adjust by. there is a one day buffer
$days=(abs($hours/$dayHours))+1;
// weeks
$adjust=604800 * floor($days / 5);
// how many days are left over
$remain=round($days % 5);
// check for negative
if ($hours<0) {
// check to see if we will land on a weekend, and make appropriate
adjustment
if (date("w",$date)<=$remain) {
$remain +=2;
}
// make the time adjustment, 86400000 microseconds in a day, times the
remaining adjustment
$date -=($adjust +(86400 * $remain));
}
else {
// check to see if we will land on a weekend, and make appropriate
adjustment
if ((6-date("w",$date))<=$remain) {
$remain+=2;
}
// make the time adjustment, 86400000 microseconds in a day, times the
remaining adjustment
$date +=($adjust +(86400 * $remain));
}
return $date;
}