Search code examples
javascriptdatetimestampunix-timestamp

Javascript timestamp is far in the future


I want to return a timestamp of the current date. I use this JavaScript commands:

var date = Date.now();
var par = document.getElementById("testdate");
par.innerHTML = date;
<p id="testdate"></p>

This returns e.g. the following timestamp: 1529489842210

But when I check the timestamp, I get the following date: 08/08/50437 @ 5:30pm (UTC)

Which is definitely not the current date^^ Or I have been sleeping way too long ;)


Solution

  • JavaScript shows in milliseconds elapsed since epoch. You have to convert it to seconds in PHP. Use:

    strtotime($date/1000);
    

    Also, in JavaScript, you can use new Date():

    preview

    Shows me the right time:

    » new Date(1529489842210)
    « Wed Jun 20 2018 11:17:22 GMT+0100 (British Summer Time)