Bug #54048 [Com]: unexpected strtotime() behavior with unix timestamps
Edit report at http://bugs.php.net/bug.php?id=54048&edit=1
ID: 54048
Comment by: tomas dot brastavicius at quantum dot lt
Reported by: sheller0 at gmail dot com
Summary: unexpected strtotime() behavior with unix timestamps
Status: Open
Type: Bug
Package: Date/time related
Operating System: Ubuntu 11.04
PHP Version: 5.3.5
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2011-02-18 20:30:02] 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$
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=54048&edit=1
Thread (3 messages)