Search code examples
phpdate64-bitmktime

Issue with php mktime() and/or php date() - years prior to 1901 - 64-bit


I understand there is an issue with php mktime(), 32bit systems, and years <1901 or >2038, however, my question is, does this issue still remain if operating on a 64 bit system?

I used the code mentioned here and determined my host is running a 64bit system.

I am gathering dates from user inputs in the following format:

$m = user selected month (2 digits); $d = user selected day (2 digits); $y = user selected year (4 digits)

Here is the code I am using to convert the input date into a unix timestamp:

$npt_date=mktime(0,0,0,$m,$d,$y);

And the code to then re-display the date in the basic format xx-xx-xxxx

$date_str=date('m-d-Y',$npt_date);

The code works fine for dates > 1901, however, when $y < 1901, the output from the date() function returns the incorrect date.

Any advice as to what I am doing wrong, if this is even possible using the mktime() and date() functions, and/or possible workaround would be greatly appreciated.

Thanks in advance.


Solution

  • It might be a good idea to start using DateTime. It's only limited backwords by the year 1000.
    Also time() and date() are limited by 2038 in the future, whereas DateTime won't have problem with the future either.
    Here is some reading on the topic.