php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #9640 strtotime() cannot properly handle some ISO 8601-compliant strings
Submitted: 2001-03-08 17:16 UTC Modified: 2001-10-27 13:46 UTC
From: derekm at hackunix dot org Assigned:
Status: Closed Package: Date/time related
PHP Version: 4.0.4pl1 OS: Linux 2.4.2
Private report: No CVE-ID: None
 [2001-03-08 17:16 UTC] derekm at hackunix dot org
I've recently run into a problem.  This problem has been addressed in bug report #9007, but I thought I would try to provide more detailed information.

First, I'm pulling ISO 8601-compliant date strings from a PostgreSQL 7.0.3.

We'll illustrate this bug with a sample date: 1999-06-24 00:01:00-05.  Or June 24, 1999, 12:01 AM CDT.

$timestamp = "1999-06-24 00:01:00-05";
$timestamp = strtotime($timestamp);
print(strftime("%A, %B %d, %Y %H:%M %Z", $timestamp));

Now, that output /should/ read "Thursday, June 24, 1999 00:01 CDT," instead it reads "Wednesday, June 23, 1999 19:06 CDT."  Even though PHP knows the timezone of the machine to be CST6CDT, it subtracts 5 hours from the time during conversion, regardless.

This is a Bad Thing(tm) because all programs should be ISO 8601-compliant.  Currently PHP4 is a lil' broken.  This should be fixed in the next release of PHP4, IMHO.  The bug probably actually resides in the code in ext/standard/parsedate.c that is borrowed from GNU Bison 1.28.  So maybe this bug should be reported to GNU's bug list as well.

In any case, here's a fix in the meantime...

If using PostgreSQL issue the command "SET DATESTYLE TO Postgres;" in all of your queries that return a timestamp.  That will cause the timestamp to be returned in this style: Thu Jun 24 00:01:00 1999 CDT.  This style will correctly parse, as we will illustrate with this example:

$timestamp = "Thu Jun 24 00:01:00 1999 CDT";
$timestamp = strtotime($timestamp);
print(strftime("%A, %B %d, %Y %H:%M %Z", $timestamp));

The output of this example is "Thursday, June 24, 1999 00:01 CDT," which is correct.

And that's all.

Derek P. Moore
derekm@hackunix.org

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-03-08 17:26 UTC] derekm at hackunix dot org
I've recently run into a problem.  This problem has been addressed in bug report #9007, but I thought I would try to provide more detailed information.

First, I'm pulling ISO 8601-compliant date strings from a PostgreSQL 7.0.3 database.

We'll illustrate this bug with a sample date: 1999-06-24 00:01:00-05.  Or June 24, 1999, 12:01 AM CDT.

$timestamp = "1999-06-24 00:01:00-05";
$timestamp = strtotime($timestamp);
print(strftime("%A, %B %d, %Y %H:%M %Z", $timestamp));

Now, that output /should/ read "Thursday, June 24, 1999 00:01 CDT," instead it reads "Wednesday, June 23, 1999 19:06 CDT."  Even though PHP knows the timezone of the machine to be CST6CDT, it subtracts 5 hours from the time during conversion, regardless.

This is a Bad Thing(tm) because all programs should be ISO 8601-compliant.  Currently PHP4 is a lil' broken.  This should be fixed in the next release of PHP4, IMHO.  The bug probably actually resides in the code in ext/standard/parsedate.c that is borrowed from GNU Bison 1.28.  So maybe this bug should be reported to GNU's bug list as well.

In any case, here's a fix in the meantime...

If using PostgreSQL issue the command "SET DATESTYLE TO Postgres;" in all of your queries that return a timestamp.  That will cause the timestamp to be returned in this style: Thu Jun 24 00:01:00 1999 CDT.  This style will correctly parse, as we will illustrate with this example:

$timestamp = "Thu Jun 24 00:01:00 1999 CDT";
$timestamp = strtotime($timestamp);
print(strftime("%A, %B %d, %Y %H:%M %Z", $timestamp));

The output of this example is "Thursday, June 24, 1999 00:01 CDT," which is correct.

And that's all.

Derek P. Moore
derekm@hackunix.org
 [2001-03-08 19:03 UTC] derekm at hackunix dot org
Another workaround for PostgreSQL (and the equivalent, if
one exists, of this on other databases will work as well):

Run your query using the date_part function so that
PostgreSQL returns the UNIX timestamp instead of an ISO
8601-style timestamp.  The query would look something like this:

SELECT date_part('epoch', timestamp) AS timestamp
FROM table

Then use can just us that those records returned directly
with PHP's date, strftime, gmdate, and etc. functions.
 [2001-10-27 13:46 UTC] derick@php.net
Fixed in CVS (will be in 4.1.0).

Derick
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 02:01:28 2024 UTC