note 62390 added to function.date

From: Date: Mon, 27 Feb 2006 16:55:11 +0000
Subject: note 62390 added to function.date
Groups: php.notes 
Request: Send a blank email to php-notes+get-104499@lists.php.net to get a copy of this message
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;
} ---- Server IP: 66.163.161.117 Probable Submitter: 68.142.19.112 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.date.php Edit -- http://master.php.net/note/edit/62390 Del: integrated -- http://master.php.net/note/delete/62390/integrated Del: useless -- http://master.php.net/note/delete/62390/useless Del: bad code -- http://master.php.net/note/delete/62390/bad+code Del: spam -- http://master.php.net/note/delete/62390/spam Del: non-english -- http://master.php.net/note/delete/62390/non-english Del: in docs -- http://master.php.net/note/delete/62390/in+docs Del: other reasons-- http://master.php.net/note/delete/62390 Reject -- http://master.php.net/note/reject/62390 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#104499) next »