php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36266 strtotime behavior change
Submitted: 2006-02-02 20:42 UTC Modified: 2006-02-13 17:07 UTC
Votes:4
Avg. Score:4.8 ± 0.4
Reproduced:4 of 4 (100.0%)
Same Version:3 (75.0%)
Same OS:1 (25.0%)
From: tim at komta dot com Assigned: derick (profile)
Status: Wont fix Package: Date/time related
PHP Version: 5CVS-2006-02-02 (snap) OS: Windows XP Pro SP2
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2006-02-02 20:42 UTC] tim at komta dot com
Description:
------------
Previously valid strtotime() attempts no longer parse to a time.  This worked prior to PHP 5.1, I believe.

Reproduce code:
---------------
<?php
echo strtotime('02/02/2006 11');
?>

Expected result:
----------------
1138896000 (with the server timezone set to EST)

Actual result:
--------------
FALSE

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-02-02 20:57 UTC] tony2001@php.net
Assigned to the maintainer.
 [2006-02-02 21:25 UTC] tim at komta dot com
strtotime('11')  exhibits the same behavior change - seems that strtotime can no longer handle only 2 digits for a time.
 [2006-02-02 22:29 UTC] judas dot iscariote at gmail dot com
$php5-debug strtotime.php

bool(false)
PHP 5.1.3-dev (cli) (built: Feb  2 2006 18:19:34) (DEBUG)

$php4-debug strtotime.php
int(1138888800)
PHP 4.4.3-dev (cli) (built: Feb  1 2006 13:49:49) (DEBUG)
 [2006-02-13 09:36 UTC] derick@php.net
Guessing that two consequetive numbers mean an hour is very strange. It could just as well have been the day of the month, the month or even the year. That this worked in older versions is most likely a sideeffect of something and I do not plan to made a hack for this in the new parser.
 [2006-02-13 15:25 UTC] tim at komta dot com
Unintentional changes in function behavior = bug.  This whole strtotime "update" has caused nothing but trouble for me.

I disagree that it's "very strange".  Ask the next 10 people you see what today's date is.  If ANY of them just say "13", I'll be extremely surprised.  Ask them what year it is.  I guarantee no one will say "6".  Conversely, in basic conversation, asking what time it is will often get you an integer in response.

The purpose of this function is to take strings and convert them to times.  Except now, it only converts SOME strings, breaking functionality with previous versions.
 [2006-02-13 15:33 UTC] derick@php.net
The documentation says "strtotime -- Parse about any English textual datetime description into a Unix timestamp". "11" is clearly not a textual datetime description.
 [2006-02-13 15:38 UTC] tim at komta dot com
By that logic, "1100" shouldn't work either, and yet, it does.

Intriguingly, it works, not to the YEAR 1100, but to a time, 11:00:00 AM.
 [2006-02-13 17:07 UTC] tim at komta dot com
--WORKAROUND--

For anyone wishing to replicate the prior behavior, I wrote this regex to use with preg_replace() immediately prior to calling the strtotime() on your value.

$value = preg_replace('#(.*)(^|\s)([0-9]{1,2})$#', '$1$2${3}00', $value);
$timestamp = strtotime($value);

This function call adds two zeroes to a 1 or two digit number either on it's own (11) or trailing a whitespace character (2/13/06 11).  It will not match a two-digit year (ie, in 2/13/06, it will not match "06")
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC