28
How to fix this bug?
echo date('c', mktime(1, 2, 3, 4, 5, 2038));
Upshot
1970-01-01T00:00:00+00:00
28
How to fix this bug?
echo date('c', mktime(1, 2, 3, 4, 5, 2038));
Upshot
1970-01-01T00:00:00+00:00
15
This bug is caused by the use of 4bytes Signed integer used for quick accounts.
The problem:
There is no no universal solution to the problem of the year 2038, any
change in the definition of time_t
may result in compatibility issues
in any application
For example, changing time_t to an unsigned 32-bit integer, which would extend the range to the year 2106, would negatively affect programs that store, retrieve, or manipulate dates before 1970, since such dates are represented by negative numbers. Increasing the size of the type time_t
64-bit on an existing system would cause incompatible changes to the layout of the interface structures and the function binary.
7
Just to complement the answer, I want to point out that with the class DateTime
I didn’t have that problem.
Behold:
$date = new DateTime('+1000 years')
echo $date->format('c'); // 3015-11-25T09:20:54-02:00
Tested on command line with psysh
.
Updating: In the ideone.com
, only the form highlighted in my example works. The form using the function date
glitch.
Observing: All this will work perfectly with DateTime
, unless you use it DateTime::getTimestamp
.
A good way to test these limitations can be done through constants PHP_INT_MAX
, which can probably be changed in different versions of PHP.
Test 1 psysh
, PHP 5.5.9 Ubuntu:
$date = new DateTime();
$date->setTimestamp(PHP_INT_MAX);
var_dump($date->format('c')); // 219250468-12-04T13:30:07-02:00
var_dump(PHP_INT_MAX); // int(9223372036854775807)
Test 2 ideone.com
:
$date = new DateTime();
$date->setTimestamp(PHP_INT_MAX);
var_dump($date->format('c')); // 2038-01-19T03:14:07+00:00
var_dump(PHP_INT_MAX); // int(2147483647)
Note that in both cases, different results were returned. So I can also assume that, because of the maximum size of the integer processed in PHP, this can affect the behavior of the functions.
+1 Here worked cool too. this Datetime uses 64bits? do not know how she does this magic.
I don’t know @Gabrielrodrigues. But check out what I found http://stackoverflow.com/questions/5319710/accessing-dates-in-php-beyond-2038
I updated the reply @Gabrielrodrigues. See the important information from PHP_INT_MAX
In this post he says that "Datetime internally represents the components of time independently. Thus, it is not susceptible to the limitation of 2038." but there is nothing in the php documentation and I found nothing in other articles, the.O
3
Use the class methods \DateTime
. Thus, the dates generated by your application are not limited by the architecture that PHP is running (in this case, 32 bits).
Good to know this information, Rodrigo. Another reason to use the Object, instead of the Function
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
Report to PHP ;)
– Wallace Maxters
It is not php bug... This is the timestamp deadline
– Daniel Omine
there is yes: http://stackoverflow.com/questions/6303972/format-date-whose-timestamp-exceeds-the-int-limit
– Filipe Moraes