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

Bug #16954 Issue with setDate() with unix timestamp argument
Submitted: 2009-12-28 10:24 UTC
From: steve_tp Assigned:
Status: Open Package: Date (version 1.4.7)
PHP Version: 5.2.1 OS: CentOS
Roadmaps: (Not assigned)    
Subscription  


 [2009-12-28 10:24 UTC] steve_tp (Steve MacMinn)
Description: ------------ setDate does not appear to behave correctly when setting with a unix timestamp. It gets the timezones messed up. I think I've tracked this down. The following code appears in Date::setDate: } elseif (is_numeric($date)) { // UNIXTIME $this->setDate(date("Y-m-d H:i:s", $date)); } else { The problem is that this code always sets the time as local time, because that's what the php date() function returns, and the "Y-m-d H:i:s' specifier does not include a GMT offset. The line that invokes $this->setDate should be changed to: $this->setDate(date("c", $date)); Which will set the date in ISO standard format with the proper GMT offset. Test script: --------------- <?php include_once('lib/Date/Date.php'); $timestamp = 1259066851; // Tuesday, November 24th 2009, 12:47:31 (GMT) $accountTimeZone = "US/Eastern"; putenv("TZ=EST"); echo "Testing Date with a timestamp of $timestamp.<br />"; echo "This should returnn a GMT time of Tuesday, November 24th 2009, 12:47:31 +00:00<br >"; $alertTime = new Date(); $alertTime->setDate($timestamp); $alertTime->setTZByID("GMT"); $alertTimeGMTString = $alertTime->format("%D %r %O"); $alertTime->convertTZByID($accountTimeZone); $alertTimeLocalDate = $alertTime->format("%D"); $alertTimeLocalDisplay = $alertTime->format("%r"); $alertTimeLocalString = $alertTime->format("%D %r %O"); $tzAfter = getenv("TZ"); echo "<br /><br />Alert timestamp: $timestamp, ($alertTimeGMTString) GMT, Account TZ: $alertTimeLocalString<br />"; echo "<br />" . "Account timezone is: $accountTimeZone <br />" . "Alert time GMT is: $alertTimeGMTString ($alertTimeGMT)<br />" . "Alert time local is: $alertTimeLocalString <br />"; ?> Expected result: ---------------- Testing Date with a timestamp of 1259066851. This should returnn a GMT time of Tuesday, November 24th 2009, 12:47:31 +00:00 Alert timestamp: 1259066851, (11/24/2009 12:47:31 PM +00:00) GMT, Account TZ: 11/24/2009 07:47:31 AM -05:00 Account timezone is: US/Eastern Alert time GMT is: 11/24/2009 12:47:31 PM +00:00 () Alert time local is: 11/24/2009 07:47:31 AM -05:00 Actual result: -------------- Testing Date with a timestamp of 1259066851. This should returnn a GMT time of Tuesday, November 24th 2009, 12:47:31 +00:00 Alert timestamp: 1259066851, (11/24/2009 07:47:31 AM +00:00) GMT, Account TZ: 11/24/2009 02:47:31 AM -05:00 Account timezone is: US/Eastern Alert time GMT is: 11/24/2009 07:47:31 AM +00:00 () Alert time local is: 11/24/2009 02:47:31 AM -05:00

Comments