Why don't PHP and Javascript's timestamps match?

If I do

alert(new Date(1313690400000))

returns: Thu Aug 18 2011 13:00:00 GMT-0500 (CDT)

however, PHP

echo date('Y-m-d H:i:s', 1313690400000);

returns: 1951-12-14 05:50:24

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

JavaScript uses milliseconds as a timestamp, whereas PHP uses seconds. As a result, you get very different dates, as it is off by a factor 1000.

So remove three zeroes at the PHP side:

echo date('Y-m-d H:i:s', 1313690400);

Solution 2

Javascript Date is milliseconds since Epoch, whereas PHP date uses unix timestamp which is in seconds.

So to get the same date in php, divide by 1000 first

Solution 3

PHP’s date/time functions use the number of seconds since the epoch, while Javascript uses the number of milliseconds. In your php func:

echo date('Y-m-d', 1313690400000 / 1000);

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply