PHP date() fails for high timestamps on 32bit

Asked
Active3 hr before
Viewed126 times

8 Answers

90%

Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers , By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy. ,The timestamp on a 32bit system goes only untill 2.147.483.647. So if you have a bigger value you come over the integer maximal length. The maximum here is the year 2038. ,Connect and share knowledge within a single location that is structured and easy to search.

Try this:

< ? php

$dt = new DateTime('@4567743118');
$date = $dt - > format('Y-m-d');

echo $date;
88%

If I echo the timestamp (when stored in variable), it shows fine, but date() destroys it (casting to int32, I assume).,I have some code for converting some timestamps stored as strings into datetime objects and noticed today exceptions when it converts dates with an int timestam...,The bad result is obtained when running the script on a 32bit platform. I think it's the famous Y2038 issue, but how should I fix it?,I am of of the understanding that due to the nature that PHP represents dates using milliseconds, you cannot represent dates past 2038. I have a problem where I...

I've stumbled upon this issue:

< ? php

echo date('r', 4567743118);
load more v
72%

For these and other reasons that it could be useful to find an alternative to the UNIX timestamp and the many PHP calendar functions that rely on it.,In file systems, a timestamp may mean the stored date and time of creation or modification of a file.,In computing, a timestamp refers to the use of a number to represent a time value, so you can establish temporal order among a set of events.,Examples of systems utilizing data structures that may contain 32-bit time representations include:

65%

Due the DateTimeDefaultFormatter::formatDate() trying to format the date from a FALSE value returned by $date->getTimestamp().,UPDATE: although DateTime::format('U') returns the right negative value, $datetimeplus->setTimestamp($date->format('U')) is still incorrect.,OK, here is a rework of DateTimePlus::createFromFormat() to not rely on timestamps. All of my local testing is green.,Has the documentation page been finalized an formally published?

strtotime('Jan 19 2038');
2147472000
strtotime('Jan 20 2038');
false
load more v
75%

Early Mac OS X versions[11] are susceptible to the Year 2038 problem. ,In MySQL relational database versions prior to August 2021, built-in functions like UNIX_TIMESTAMP() will return 0 after 03:14:07 UTC on 19 January 2038.[10]. ,Players of games or apps which are programmed to impose waiting periods[6] are running into this problem when the players try to bypass the waiting period by setting the date on their devices to a date past 19 January 2038, but are unable to do so, since a 32-bit Unix time format is being used. ,Years 32,768 and 65,536 problems

Despite the modern 18–24 month generational update in computer systems technology, embedded systems are designed to last the lifetime of the machine in which they are a component. It is conceivable that some of these systems may still be in use in 2038. It may be impractical or, in some cases, impossible to upgrade the software running these systems, ultimately requiring replacement if the 32-bit limitations are to be corrected.

In MySQL relational database versions prior to August 2021, built-in functions like UNIX_TIMESTAMP() will return 0 after 03:14:07 UTC on 19 January 2038.[10].

UNIX_TIMESTAMP()
load more v
40%

To identify wheter the version of PHP that you use has the bug, you can run the following script:,If you have found this article useful and informative, please share it with your PHP developers friends, so they're aware of this bug as well.,As mentioned initially, operative systems that support the x64 architecture and use a 64-bit compiled version of PHP won't have this issue. During and after 2038, this number will exceed 231 the largest number representable by a signed long integer on 32 bit systems, causing the Year 2038 problem. As a long integer in 64 bit systems uses 64 bits, the problem does not exist on 64 bit systems that use the LP64 model. ,Till the date, the Y2K38 bug is not so widely known by the PHP developers. The year 2038 problem, usually named as "the Unix Millennium Bug" with the acronym Y2K38 (Y stands for Year, 2K for 2000 and 38 for the year) that cause some software to fail before or in the year 2038. The problem affects all software and systems (including PHP) that store system time as a signed 32-bit integer (timestamp), and interpret this number as the number of seconds since 00:00:00 UTC on January 1, 1970.

To identify wheter the version of PHP that you use has the bug, you can run the following script:

<?php
    // Define a date beyond 2038
    $dateString = '2039-01-01';
    $format = 'l d F Y H:i';
    
    // Parse a textual date/datetime into a Unix timestamp
    $date = strtotime($dateString);
    
    // Print it
    echo '<span>'. date($format, $date) .'</span>';
?>
load more v
22%

Both PHP and Oracle provide functionality manipulating dates and times. Which to use and when?by Harry Fuecks,Formatting UNIX timestamps. The date() function is used to format UNIX timestamps:,The date() function can also be useful for certain calculations:,Oracle provides three data types for storing date/time values:

 SELECT TO_DATE('20050726173102', 'YYYYMMDDHH24MISS') FROM dual
 Or to convert "Jul 26, 2005 17:13:05", I can use:
    SELECT TO_DATE('Jul 26, 2005 17:13:05', 'Mon DD, YYYY HH24:MI:SS') FROM dual
load more v
60%

Format accepted by DateTimeInterface::format(). , The optional timestamp parameter is an int Unix timestamp that defaults to the current local time if timestamp is omitted or null. In other words, it defaults to the value of time(). , Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time(). ,idate() - Format a local time/date as integer

load more v

Other "undefined-undefined" queries related to "PHP date() fails for high timestamps on 32bit"