php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54048 unexpected strtotime() behavior with unix timestamps
Submitted: 2011-02-18 20:30 UTC Modified: 2011-03-02 16:58 UTC
From: sheller0 at gmail dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.3.5 OS: Ubuntu 11.04
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: sheller0 at gmail dot com
New email:
PHP Version: OS:

 

 [2011-02-18 20:30 UTC] sheller0 at gmail dot com
Description:
------------
I'm working on a payment processor which unfortunately has to take several types of date input, as a result I need to test if a date I'm being passed is already a unix timestamp. For the most part strtotime() will return false when passed a timestamp. There are a few edge cases where it will return another timestamp sometime after year 2400 or before 2000. If this is actually the expected behavior it might be worthwhile to throw a warning in the documentation about passing strtotime() unix timestamps

Test script:
---------------
$errors = 0;
$end_date = strtotime('January 1st 2020');
$start_date = strtotime('January 1st 2001');
$bad_years = array();
while ($start_date < $end_date){
	if (strtotime($start_date) !== false){
		echo date ('Y-m-d', $start_date) . ' generates ' . strtotime($start_date) .  '  ' . date('Y-m-d', strtotime($start_date)) . "\n";
		$bad_years[] = date('Y', strtotime($start_date));
		$errors++;
	}
	$start_date += 86400;
}
print_r(array_unique($bad_years));
echo "$errors errors generated\n";

Expected result:
----------------
sam.heller@sam:/var/www/development/api/app/scripts$ php test.php 
Array
(
)
0 errors generated
sam.heller@sam:/var/www/development/api/app/scripts$ 


Actual result:
--------------
sam.heller@sam:/var/www/development/api/app/scripts$ php test.php 
2001-09-14 generates 51442009244  3600-02-18
2001-09-15 generates -62163017947  0000-02-18
2001-09-25 generates 64064790099  4000-02-18
2001-09-26 generates -49540237092  0400-02-18
...
...
...
2019-05-22 generates -24294654070  1200-02-18
2019-05-23 generates 177669838738  7600-02-18
2019-06-02 generates -11671873215  1600-02-18
2019-06-03 generates 190292619593  8000-02-18
Array
(
    [0] => 3600
    [1] => 0000
    [2] => 4000
    [3] => 0400
    [4] => 6800
    [5] => 0800
    [6] => 7200
    [8] => 1200
    [9] => 7600
    [11] => 8000
    [12] => 4400
    [14] => 8400
    [15] => 4800
    [17] => 5200
    [18] => 1600
    [20] => 2000
    [22] => 2400
    [23] => 8800
    [25] => 9200
    [26] => 5600
    [28] => 9600
    [29] => 6000
    [31] => 6400
    [32] => 2800
    [34] => 3200
)
1007 errors generated
sam.heller@sam:/var/www/development/api/app/scripts$

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-02 16:36 UTC] tomas dot brastavicius at quantum dot lt
If you want to pass a timestamp to strtotime() function you must prepend the timestamp with '@' character. See http://www.php.net/manual/en/datetime.formats.compound.php
Since you do not prepend '@' character, strtotime() assumes that you are passing other than timestamp formatted string. For example, digits "1009584000" is interpreted as 10 hour 9 minutes 58 seconds, 4000 year. Some function calls fails because no format matches given digits.
If you want to test a string against timestamp, you may use preg_match() function.
 [2011-03-02 16:58 UTC] derick@php.net
-Status: Open +Status: Bogus
 [2011-03-02 16:58 UTC] derick@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 20 15:01:36 2024 UTC