Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.5.0a4

Bug #16520 Compare returns invalid result for certain dates due to standard time conversion
Submitted: 2009-08-13 23:43 UTC
From: svanhoose Assigned:
Status: Open Package: Date (version 1.5.0a1)
PHP Version: 5.2.5 OS: Irrelevant
Roadmaps: (Not assigned)    
Subscription  


 [2009-08-13 23:43 UTC] svanhoose (Spencer Van Hoose)
Description: ------------ The Date::compare function uses incorrect logic to compare two dates. It first gets the value of each date in days (without converting to standard time). If the days are equal, it goes on to check hours, minutes, etc. However, it uses getStandardHour, etc. for those comparisons. This can cause problems for dates near day boundaries where this conversion can cause the hours to rollover to the previous day. e.g. 2009-08-07 10:00:00 and 2009-08-07 00:00:00 (both in America/Los_Angeles) The # of days are equal, so hours must be compared. For the first date getStandardHour() returns 9. On the second date, getStandardHour() returns 23. This causes compare to return that the 1st date is before the 2nd. Test script: --------------- <?php require_once 'Date.php'; $d1 = new Date("2009-08-07 10:00:00"); $d2 = new Date("2009-08-07 00:00:00"); $d1->setTZByID("UTC-0800"); $d2->setTZByID("UTC-8000"); $result = Date::compare($d1, $d2); echo "Is " . $d1->getDate() . " before " . $d2->getDate() . " ?\n"; echo ($result < 0 ? "Yes" : "No"); ?> Expected result: ---------------- The code should print No since obviously August 8th at 10 AM is not before August 8th at 12 AM. Actual result: -------------- The code prints Yes.

Comments

 [2009-08-14 00:06 UTC] svanhoose (Spencer Van Hoose)
Sorry, I set the time zone incorrectly in my test script. Please use these lines instead: ----------- $d1->setTZByID("America/Los_Angeles"); $d2->setTZByID("America/Los_Angeles");
 [2009-09-26 22:32 UTC] svanhoose (Spencer Van Hoose)
-Summary: compare sometimes returns invalid result due to standard time conversion +Summary: Compare returns invalid result for certain dates due to standard time conversion -Operating System: Windows XP Professional +Operating System: Irrelevant
 [2014-07-18 00:06 UTC] jeffdafoe (Jeff Dafoe)
Yes, the problem is the Standard time variables are used for the time portion of the compare but not for the date portion. We just noticed the same issue here. The patch for Date.php is: @@ -4297,12 +4297,12 @@ } } - $days1 = Date_Calc::dateToDays($d1->getDay(), - $d1->getMonth(), - $d1->getYear()); - $days2 = Date_Calc::dateToDays($d2->getDay(), - $d2->getMonth(), - $d2->getYear()); + $days1 = Date_Calc::dateToDays($d1->getStandardDay(), + $d1->getStandardMonth(), + $d1->getStandardYear()); + $days2 = Date_Calc::dateToDays($d2->getStandardDay(), + $d2->getStandardMonth(), + $d2->getStandardYear()); if ($days1 < $days2) return -1; if ($days1 > $days2)