PHP wont work after 2038

Added on: Tuesday 11th June 2013

That is a bit of an attention grabbing headline but there is at least one function in PHP that doesn't like dates after 2038.

I've recently been working on a web based CRM system for a client who is an Independent Financial Adviser.

They rang me up the other day and told me they couldn't save the date in a particular field of a mortgage record.

I duly logged on to their server (it is hosted on their Intranet) and tried to edit the record in question. As they had said when saved it displayed 31/12/1969.

I was using strtotime to convert the database value before displaying it. The function converts the date into the number of seconds since 1st Jan 1970 so that date I was seeing implied an empty field in the database so I initially thought that it was just the form not saving the value.

I went through the code time and time again and tried a few tests on other records and all seemed fine.

It was only when I looked in the database that I saw the date had been stored (2041-05-20) but for some reason wasn't being displayed.

It turns out that after a date in 2038, the number of seconds since 1st Jan 1970 is too big to store as an integer value so it was returning nothing.

There is a fairly easy solution though which is to use the date_create function to return a DateTime object and then use the date_format function to format it.

$date = date_create($mortgageEndDate);
$end_date = date_format($date, "d-m-Y");

The only caveat is that this requires php 5.2 or greater.

However, it is worth thinking about because 2038 isn't that far away especially when you are dealing with 25 year mortgages!