Adnan
Adnan

Reputation: 26350

Linux timestamp to PHP

I have the following timestamp:

1342259667654

which when converted with http://www.epochconverter.com/ gives:

Assuming that this timestamp is in milliseconds:
GMT: Sat, 14 Jul 2012 09:54:27 GMT
Your time zone: 14. juli 2012 11:54:27 GMT+2

And that is the correct time, but when using:

echo date("Y-m-d H:i:s", 1342259667654);

I get the following date:

1904-07-24 10:22:47

How can I get with PHP the exact date out of this time stamp?

Upvotes: 5

Views: 1146

Answers (3)

liliumdev
liliumdev

Reputation: 1179

$timestamp = 1342259667; 
$dt = new DateTime("@$timestamp");  // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s');

You can also do it this way.

Upvotes: 2

hjpotter92
hjpotter92

Reputation: 80639

The value 1342259667654 is actually in miliseconds, while PHP's date() function is unable to handle miliseconds value. Hence the weird output.

Upvotes: 0

davidethell
davidethell

Reputation: 12018

Your timestamp needs to be divided by 1000:

echo date("Y-m-d H:i:s", 1342259667654/1000);

Upvotes: 6

Related Questions