First of all this is not a duplicate of:
https://stackoverflow.com/questions/9756120/how-do-i-get-a-utc-timestamp-in-javascript
https://stackoverflow.com/questions/8047616/get-a-utc-timestamp-in-javascript
and some more. As none of them resolved my problem.
I’m doing some verifications on server side based on client sent Unix timestamp, this two should match (or 1-2 secs difference, doesn’t matter), but it doesn’t.
On the server side I’m getting the timestamp with time()
(php), so I can compare with the timestamp sent from client.
And should match with the one the client sends, but it doesn’t on my phone, chrome browser and i’m sure that it doesn’t with other systems.
alert(new Date().getTime()); alert(Date.now()); alert(new Date().valueOf()); alert(Date.now()); var x = new Date(); alert(x.getTime() + x.getTimezoneOffset()*60);
https://jsfiddle.net/mmzbne4a/6/
On my desktop the output is:
1512035634733 1512035634733 1512035634734 1512035634734 15120356347340
Which is:
Thursday, November 30, 2017 9:53:54.734 AM
On my mobile chrome browser:
1512036893294 1512036893294 1512036893294 1512036893294 15120368932940
which is:
Thursday, November 30, 2017 10:14:53.294 AM
So, I am almost sure that is because I have my phone clock set to 20mins from now, but I would like to have a ‘global timestamp’ common to all devices all most of them.
So wrapping things up, my question is:
Is there any way that i can ensure that the timestamp sent from client will be the same (1-3 secs, margin of error) as the server have?
PS: I’d rather not have to rely on an external api to get the correct timestamp, as this force another request from the client not helping the performance of the app.