PHP date () error for high timestamps on 32bit

I came across this problem:

<?php

echo date('r', 4567743118);

      

Desired and correct result (on localhost):

Sun, 30 Sep 2114 10:31:58 +0100

Invalid result (on the remote):

Thu, 24 Aug 1978 04:03:42 +0100

A bad result is obtained when running the script on a 32 bit platform. I think this is the famous Y2038 problem, but how to fix it?

If I iterate over the timestamp (when stored in a variable) it displays fine but date()

destroys it (assuming I am using int32).

<?php
$a = 4567743118;
echo $a;

      

4567743118

[this is PHP 5.4.4 from debian repos if applicable]

+3


source to share


2 answers


Try the following:

<?php

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

echo $date;

      



x86

+2


source


The timestamp on a 32-bit system only goes up to 2.147.483.647. Therefore, if you have a larger value, you get the maximum maximum length. The maximum here is 2038.



If you need large dates, you must use date and time.

+1


source







All Articles