Cream Whipped Airplane
Cream Whipped Airplane

Reputation: 1367

Converting date to milliseconds to date

I ran into a strange issue. I am sending a date that I converted to milliseconds using Javascript to my controller. There, I convert the milliseconds to a time stamp in PHP. It seems to get the date part of it right, but the time is totally off. Can anyone please tell me what am I missing? The code is below.

javascript

var d = new Date("13 October 2014 11:13:00");    
document.write(d.getTime() + " milliseconds since 1970/01/01");

result

1413191580000

php

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

result

2014-10-13 02:13:00

Upvotes: 1

Views: 162

Answers (2)

Steve11235
Steve11235

Reputation: 2923

UNIX-style date/times (millis since 1/1/1970) use UTC (AKA GMT, Zulu) timezone. The problem is that many methods "help" you by converting that date/time to the local timezone, also taking into account daylight savings. So, a date/time created in the winter will display an hour earlier in the summer, if the DST is effect.

Generally, you won't be able to control the client or the server timezone settings, so the solution is to be aware of conversion issues. There is no simple solution, unfortunately.

Upvotes: 0

Cameron Tinker
Cameron Tinker

Reputation: 9789

I'm guessing this is happening because you have a date with a different timezone returned from the client than the server. JavaScript's Date.prototype.getTime() returns a UTC timestamp according to the ECMAScript standard (§15.9.1.1). Standardize the time zones between client and server and then you can have dates be the same in both. I always deal with dates in UTC on the server and then do client side timezone localizations.

Upvotes: 5

Related Questions