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

Bug #16822 setTZByID() does not behave as expected
Submitted: 2009-11-23 01:34 UTC
From: steve_tp Assigned:
Status: Open Package: Date (version 1.4.7)
PHP Version: 5.2.5 OS: CentOS
Roadmaps: (Not assigned)    
Subscription  


 [2009-11-23 01:34 UTC] steve_tp (Steve MacMinn)
Description: ------------ Perhaps this is an expectation problem. I instantiate a new Date object with a timestamp $x = new Date(time()); and then set the timezone $x->setTZByID("GMT"); I expect the timestamp to remain the same, and the date to now be reported in GMT, but the date and time are now reported inconsistently. Am I interpreting this wrong somehow? Test script: --------------- <?php /** Test of Date class demonstrating how setTZByID() does not work as expected. */ include ("/Date.php"); // initialize object $timestamp = 1258918447; $d = new Date($timestamp); $d->setTZByID("GMT"); $timestampGMT = $d->getTime(); echo "Local Time: " . date("m/d/Y h:i:s A O") . " timestamp = $timestamp <br />"; echo "Time in GMT: " . $d->format("%D %r %O") . " timestamp = $timestampGMT <br />"; ?> Expected result: ---------------- Local Time: 11/22/2009 02:34:07 PM -0500 timestamp = 1258918447 Time in GMT: 11/22/2009 07:34:07 PM +00:00 timestamp = 1258918447 Actual result: -------------- Local Time: 11/22/2009 02:34:07 PM -0500 timestamp = 1258918447 Time in GMT: 11/22/2009 02:34:07 PM +00:00 timestamp = 1258918447 Note: on 2nd line, timestamp and time are now inconsidtent.

Comments

 [2009-11-23 01:49 UTC] steve_tp (Steve MacMinn)
Corrections to test script. /** Test of Date class demonstrating how setTZByID() does not work as expected. */ include ("Date.php"); // initialize object $timestamp = 1258918447; $d = new Date($timestamp); $d->setTZByID("GMT"); $timestampGMT = $d->getTime(); echo "Local Time: " . date("m/d/Y h:i:s A O", $timestamp) . " timestamp = $timestamp <br />"; echo "Time in GMT: " . $d->format("%D %r %O") . " timestamp = $timestampGMT <br />";
 [2010-06-25 01:33 UTC] pswanson (Patrick Swanson)
This is because getDate(DATE_FORMAT_UNIXTIME) uses gmmktime, which causes the date being returned to first be converted to local time. If you change it to mktime, it should behave as expected.