MightyPork
MightyPork

Reputation: 18871

PHP date() fails for high timestamps on 32bit

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]

Upvotes: 3

Views: 120

Answers (2)

Bas Peeters
Bas Peeters

Reputation: 3354

Try this:

<?php

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

echo $date;

run code on x86 machine

Upvotes: 2

Ren&#233; H&#246;hle
Ren&#233; H&#246;hle

Reputation: 27305

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.

Upvotes: 1

Related Questions