3

I've stumbled upon this issue:

<?php

echo date('r', 4567743118);

Desired & correct result (on localhost):

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

Incorrect result (on remote):

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

The bad result is obtained when running the script on a 32bit platform. I think it's the famous Y2038 issue, but how should I fix it?

If I echo the timestamp (when stored in variable), it shows fine, but date() destroys it (casting to int32, I assume).

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

4567743118

[if relevant, it's PHP 5.4.4 from debian repos]

MightyPork
  • 18,270
  • 10
  • 79
  • 133

2 Answers2

2

Try this:

<?php

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

echo $date;

run code on x86 machine

Bas Peeters
  • 3,269
  • 4
  • 33
  • 49
1

The timestamp on a 32bit system goes only untill 2.147.483.647. So if you have a bigger value you come over the integer maximal length. The maximum here is the year 2038.

If you need bigger dates you have to use datetime.

René Höhle
  • 26,716
  • 22
  • 73
  • 82