php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #19383 Date->converttz($tz) fails for timezones in western hemisphere
Submitted: 2002-09-12 20:04 UTC Modified: 2002-09-12 22:12 UTC
From: asutton at sbcglobal dot net Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 4.2.3 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: asutton at sbcglobal dot net
New email:
PHP Version: OS:

 

 [2002-09-12 20:04 UTC] asutton at sbcglobal dot net
When using Date.php, the converttz($tz) function does not properly convert the date if the timezone offset from gmt
is negative. The problem appears to be caused by a typo in the converttz function which causes subtractSeconds() to be
called with a negative value. In a nutshell, the abs() should be applied to the subtractSeconds() argument, not the addSeconds(). I have included a patch which has completely eliminated the problem, as far as I can test. One could also make a case for ammending subtractSeconds() and addSeconds() to catch negative values.

Here's a script which demonstrates the problem:

---Cut Here---
<html>
<body>
<?php

require_once("Date.php");
require_once("Date/TimeZone.php");

$mydate = new Date("2002-05-06 20:21:22");
$mylocaltz = new Date_Timezone("US/Central");

print "UTC: " . $mydate->format("%Y-%m-%d %H:%M:%S") . "<br>\n";

$mydate->converttz($mylocaltz);

print "CDT: " . $mydate->format("%Y-%m-%d %H:%M:%S") . "<br>\n";

?>
</body>
</html>
---Cut Here---

The output on my system is:

UTC: 2002-05-06 20:21:22
CDT: 2002-05-06 25:21:22

Note that it is going the wrong way, and that the hours is greater than 23! This isn't even a valid date.

Here's the patch:

---Cut Here---
*** ../Date.php Fri Jun 21 20:58:20 2002
--- Date.php    Thu Sep 12 17:16:42 2002
***************
*** 440,452 ****
          } else {
              $this->addSeconds(intval(abs($this->tz->getOffset($this)) / 1000))
;
          }
          // convert UTC to new timezone
          if($tz->getOffset($this) > 0) {
!             $this->addSeconds(intval(abs($tz->getOffset($this)) / 1000));
          } else {
!             $this->subtractSeconds(intval($tz->getOffset($this) / 1000));
          }
          $this->tz = $tz;
      }

      /**
--- 440,452 ----
          } else {
              $this->addSeconds(intval(abs($this->tz->getOffset($this)) / 1000))
;
          }
          // convert UTC to new timezone
          if($tz->getOffset($this) > 0) {
!             $this->addSeconds(intval($tz->getOffset($this) / 1000));
          } else {
!             $this->subtractSeconds(intval(abs($tz->getOffset($this)) / 1000));
          }
          $this->tz = $tz;
      }

      /**
---Cut Here---

Thanks,

Allan

--
Allan Sutton
asutton@sbcglobal.net




Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-09-12 20:16 UTC] asutton at sbcglobal dot net
Nevermind. It looks like this has already been fixed in version 1.6 of Date.php, so it'll be ok in the next release.

Thanks,

Allan
 [2002-09-12 22:12 UTC] sniper@php.net
okay..

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 09 23:01:33 2025 UTC