Is Your PHP Application Affected by the Y2K38?

  • Avinash
  • 4
  • Aug 27, 2010
  • Tutorial

What if you run below php code in your system?

', date($format, $mydate1), '

'; ?>
<?php
  $date = '2040-02-01';
  $format = 'l d F Y H:i';
  $mydate1 = strtotime($date);
  echo '<p>', date($format, $mydate1), '</p>';
?>

With luck, you’ll see “Wednesday 1 February 2040 00:00″ displayed in your browser. If you’re seeing a date in the late 60′s or early 70′s, your PHP application may be at risk from the Y2K38 bug!

What is the Y2K38 bug?

The year 2038 problem (also known as Unix Millennium Bug, Y2K38 or Y2.038K by analogy to the Y2K problem) may cause some computer software to fail before or in the year 2038.

Y2K38, or the Unix Millennium Bug, affects PHP and many other languages and systems which use a signed 32-bit integer to signify 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.

However, developers thought that way about the Millennium bug the 1970′s and 80′s. Also, any web application which handles long-term future events could be at risk. For example, a typical mortgage runs for 25 years. Pensions and savings plans can be far longer.

Are there alternative options?

Fortunately, PHP introduced a new DateTime class in version 5.2 (experimental support was available in 5.1 and be aware that some methods were introduced in 5.3)

', $mydate2->format($format), '

'; ?>
<?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. I might have paid off my mortgage by then!
It may not be worth upgrading existing applications, but you should certainly consider the DateTime class when planning your next project.

Read Full Article at : Site Point

Related Posts

Written by Avinash

Avinash Zala currently working at a leading web development company as a Sr. PHP Web Developer. He is the founder of the Xpert Developer. He has completed B.E. in Information Technology. Born and brought up in Ahmedabad. He loves to code and coding is all about passion for him. Follow him on twitter at Avinash Zala & Xpert Developer.

View all posts by:

  • Bachor

    Wow, that was a good read through. Finally, somebody who really thinks and understands what they’re writing about. Quite rare nowadays, especially on the net. I saved your website and will make sure to keep coming back here if this is how you usually post. Thanks, keep it up!

    • buggedcom

      This DateTime stuff is interesting. I didn’t realise there was a coming apocalypse that I should be aware of. So thanks.

  • http://www.anthonyw.net Anthony

    If it is y2k38 and the entire world hasn’t been moved to sixty four bit registers for integers then I could see this being a problem.

  • Pingback: Is Your PHP Application Affected by the Y2K38? | Xpert Developer | PHP: My ZCE Preparation Blog