Cuelogic Blog Icon
Cuelogic Career Icon
Home > Blog > All > Unix Millennium Bug Affecting Php

Unix Millennium Bug Affecting Php

Unix Millennium Bug Affecting Php

Year 2038 problem

The year 2038 problem may cause some computer software to fail at some point near the year 2038. The problem affects all software and systems that both store system time as a signed 32-bit integer, and interpret this number as the number of seconds since 00:00:00 UTC on Thursday, 1 January 1970. The furthest time that can be represented this way is 03:14:07 UTC on Tuesday, 19 January 2038. Times beyond this moment will "wrap around" and be stored internally as a negative number, which these systems will interpret as a date in December 13, 1901 rather than January 19, 2038. This is caused by integer overflow. The counter "runs out" of usable digits, "increments" the sign bit instead, and reports a maximally negative number (continuing to count up, toward zero). This is likely to cause problems for users of these systems due to erroneous calculations (source: wikipedia)

How it affects PHP & other programming languages?
Y2K38, or the Unix Millennium Bug, affects PHP and many other languages and systems which use a signed 32-bit integer to represent dates as the number of seconds since 00:00:00 UTC on 1 January 1970. The furthest date which can be stored is 03:14:07 UTC on 19 January 2038. Beyond that, the left-most bit is set and the integer becomes a negative decimal number — or a time prior to the epoch.

Will 64-bit save us?
Probably. If you’re using a 64-bit OS with a compiled 64-bit edition of PHP, your application shouldn’t be affected. I’d recommend you test it, though. A signed 64-bit number gives a maximum future date which is 21 times greater than the current age of the universe — 292 billion years, give or take a day or two.

Are there alternative options?
Fortunately, PHP introduced a new DateTime class in version 5.2

 

<?php
$date = '2040-02-01';

$format = 'l j F Y H:i';

$mydate2 = new DateTime($date);

echo '<p>'.$mydate2->format($format).'</p> ';

?>
DateTime does not suffer from Y2K38 problems and will happily handle dates up to December 31, 9999.

From MySql point of view:

When using UNIX Timestamps to store dates, you are actually using a 32 bits integers, that keeps count of the number of seconds since 1970-01-01 ; see Unix Time

That 32 bits number will overflow in 2038. That's the 2038 problem.
To solve that problem, you must not use a 32 bits UNIX timestamp to store your dates -- which means, when using MySQL, you should not use TIMESTAMP, but DATETIME

You can find more discussion about this on below resources:

 

http://stackoverflow.com/questions/5319710/accessing-dates-in-php-beyond-2038

http://stackoverflow.com/questions/15916219/php-strtotime-and-dates-beyond-19-jan-2038

http://stackoverflow.com/questions/2012589/php-mysql-year-2038-bug-what-is-it-how-to-solve-it

http://stackoverflow.com/questions/3266077/php-strtotime-is-returning-false-for-a-future-date

http://www.sitepoint.com/is-your-php-application-affected-by-the-y2k38-bug/