php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55575 strtotime() returns erroneous result on nonsense DATE_RSS format date strings
Submitted: 2011-09-03 04:04 UTC Modified: 2014-07-17 19:26 UTC
From: vlayne dot tech+bugsphp at gmail dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.3.8 OS: Linux & Darwin
Private report: No CVE-ID: None
 [2011-09-03 04:04 UTC] vlayne dot tech+bugsphp at gmail dot com
Description:
------------
(Note: I was only able to test on 5.2.16, 5.3.0, and 5.3.4.  Since there is no 
evidence of it being fixed in the changelog of 5.3.8, I assume it's still there.)

The ninth of May, 2010, was a Sunday.  However, if you assert to strtotime() that 
it was a Monday, it will return the Unix timestamp for Monday, May 10th.  Likewise 
if you assert the ninth was a Tuesday, it will return the timestamp for the 11th; 
a Wednesday the 12th, and so on right up to returning a Unix timestamp for 
Saturday, 15 May 2010 if you provide it with "Saturday, 9 May 2010".

Presumably, the correct behavior is one of (1) to throw an error given a self-
contradictory date, (2) to return nothing at all, or (3) to ignore the day-of-the-
week element and return the Unix timestamp for the date specified (e.g. 9 May 
2010).  Returning the timestamp for another date altogether is wrong.

Test script:
---------------
$datestring="9 May 2010 04:24:36 GMT";
print "\"\": ".strtotime($datestring)." ".date($dateformat, strtotime($datestri\
ng)) ."\n";

$datestring="Sun, 9 May 2010 04:24:36 GMT";
print "Sun: ".strtotime($datestring)." ".date($dateformat, strtotime($datestrin\
g)) ."\n";

$datestring="Mon, 9 May 2010 04:24:36 GMT";
print "Mon: ".strtotime($datestring)." ".date($dateformat, strtotime($datestrin\
g)) ."\n";

$datestring="Tue, 9 May 2010 04:24:36 GMT";
print "Tue: ".strtotime($datestring)." ".date($dateformat, strtotime($datestrin\
g)) ."\n";


Expected result:
----------------
"": 1273379076 Sun, 09 May 2010 00:24:36 -0400
Sun: 1273379076 Sun, 09 May 2010 00:24:36 -0400
Mon: 1273379076 Sun, 09 May 2010 00:24:36 -0400
Tue: 1273379076 Sun, 09 May 2010 00:24:36 -0400

(Alternatively:


"": 1273379076 Sun, 09 May 2010 00:24:36 -0400
Sun: 1273379076 Sun, 09 May 2010 00:24:36 -0400
Mon: 
Tue: 
)

Actual result:
--------------
"": 1273379076 Sun, 09 May 2010 00:24:36 -0400
Sun: 1273379076 Sun, 09 May 2010 00:24:36 -0400
Mon: 1273465476 Mon, 10 May 2010 00:24:36 -0400
Tue: 1273551876 Tue, 11 May 2010 00:24:36 -0400

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-07-17 12:40 UTC] datibbaw@php.net
How are we supposed to properly handle nonsense? How are you sure that what you have proposed is the correct way of dealing with this? Have you seen other implementations that do this?
 [2014-07-17 14:15 UTC] salathe@php.net
-Status: Open +Status: Not a bug
 [2014-07-17 14:15 UTC] salathe@php.net
Date strings sent to strtotime() can consist of absolute and relative parts. 

For "Tue, 9 May 2010 04:24:36 GMT":
 - absolute parts are processed first "9 May 2010 04:24:36 GMT"
 - then, the relative part "Tue" is processed

This last step, as detailed on the Relative Formats [1] manual page, shifts the date to the 11th, which is the next Tuesday.

This behaviour makes much more sense than ignoring parts of the subject date string, or disallowing relative date parts and instead throwing an error.

[1] http://php.net/datetime.formats.relative
 [2014-07-17 19:16 UTC] vlayne dot tech+bugsphp at gmail dot com
> How are we supposed to properly handle nonsense?

By throwing an error.  Returning different nonsense is failing silently.
 [2014-07-17 19:26 UTC] vlayne dot tech+bugsphp at gmail dot com
> This behaviour makes much more sense than ignoring parts of the subject date 
> string, or disallowing relative date parts and instead throwing an error.

No, it does not.  There is no form of English in which "Tue, 9 May 2010" is a synonym for "10 May 2010".  It is as invalid an expression of a date as "Tue, 41 Bleen 2010", and should throw an error.

The use of _dayofweek_ as relative is not generally supported in English; it can be used that way only if the expression is called with the definite pronoun _the_ and the _dayofweek_ is followed by either the _after_ or _before_ operators.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 14 08:01:30 2025 UTC