note 62390 deleted from function.date by nlopess

From: Date: Mon, 27 Feb 2006 17:01:22 +0000
Subject: note 62390 deleted from function.date by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-104500@lists.php.net to get a copy of this message
Note Submitter: php at creuzer dot com ---- Brian W. Spolarich's Business Day Difference Counter has a bug in it. On months where the last day is on the weekend, his code counts the last business day as 1 instead of 0, so it adds an extra day of "difference" (because part of his date checking logic in in the while loop, the last business day isn't the end of the month, so it gets counted). 

The simple fix is to work backwards from the end of the month towards the initial date given.

The modifiend function is below:

function date_business_day_diff($start_date, $end_date)
{
 $business_days = 0;

 if ($start_date < $end_date) {
 $date1 = $start_date;
 $date2 = $end_date;
 } else {
 $date1 = $end_date;
 $date2 = $start_date;
 }
 
 while ($date1 < $date2) {
 $thedate = getdate($date2);
 // Skip saturday or sunday.
 if (($thedate["wday"] != '0') and ($thedate["wday"] != '6')) {
 $business_days++; // add a day to the difference counter
 }
 $date2 -= 86400; // subtract a day - work backwards so that months that end in a weekend are correctly tabulated.
 }
 return $business_days;
}

« previous php.notes (#104500) next »