PHP Classes

Date Span Calculator bug?

Recommend this page to a friend!

      Date Span  >  All threads  >  Date Span Calculator bug?  >  (Un) Subscribe thread alerts  
Subject:Date Span Calculator bug?
Summary:Negative days displayed - bug?
Messages:2
Author:Greg Sloman
Date:2005-03-24 12:03:18
Update:2005-03-25 00:07:53
 

  1. Date Span Calculator bug?   Reply   Report abuse  
Picture of Greg Sloman Greg Sloman - 2005-03-24 12:03:18
If you put in these two dates:

$date = "2005-03-23";
$date2 = "2006-01-22";
echo "$date - $date2: ".$ds->calculate_span($date, $date2);

The result is 9 months and -1 days.

If you decrease $date2 to 2006-01-01 for example, the result becomes 9 months and -22 days but a day before (2005-12-31) correctly results in 9 months and 8 days.

  2. Re: Date Span Calculator bug?   Reply   Report abuse  
Picture of Victor Hugo Cardenas Varon Victor Hugo Cardenas Varon - 2005-03-25 00:07:53 - In reply to message 1 from Greg Sloman
Hi
You are right, this is a bug.

however, with the dates:
$date = "2005-03-23";
$date2 = "2006-01-22";
echo "$date - $date2: ".$ds->calculate_span($date, $date2);

the result expected is 9 months and 30 days an not
8 months and n (30) days.

I search the cause in the code, and i found
that the problem was in the function days_in_month($month,$year)
returning 0 days in month when the number of the month was zero (0),
because it was returning the element with index -1 in array $ndays
i have fixed the bug including

if($month==0) {
$month=12;
}

in the begining of the function days_in_month($month,$year).

The final code of the days_in_month($month,$year) function is:

function days_in_month($month,$year) {
/* March 24, 2005
Addedd to fix Negative days displayed bug
Bug found by Greg Sloman
The bug was in function days_in_month($month,$year)
returning 0 days in month when the number of the month was zero (0),
because it was returning the element with index -1 in array $ndays
*/
if($month==0) {
$month=12;
}
/****** End of Addedd to fix Negative days displayed bug **********/
$ndays = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
if ($month==2 && $this->is_leapyear($year)) {
return 29;
}
else return $ndays[$month-1];
}//end function days_in_month

i'll wait some days before i update the package to see
if the fix is not causing some problem in another situation.

Thanks Greg