D
D
DeusModus2012-07-18 14:57:09
PHP
DeusModus, 2012-07-18 14:57:09

UNIX_TIMESTAMP taken from MySQL and converted to Date in JS gives weird result

Hello.

SELECT FROM_UNIXTIME(1342382400)

In mysql gives 2012-07-16 00:00:00
But
var date = new Date(1342382400*1000);
console.log(date)

Shows Sun Jul 15 2012 23:00:00 GMT+0300

Maybe I missed something and did not take into account time zones / locale?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nazar Mokrinsky, 2012-07-18
@DeusModus

Most likely yes, time zones, the difference is only one hour.
MySQL works in the time zone of the server, and JS in the time zone of the browser, that is, the client computer.
Apparently the server is running at GMT+0400

A
Alexey Zhurbitsky, 2012-07-18
@blo

Yes, the difference in time zones is equal to one hour.

FROM_UNIXTIME() functions return values ​​in the connection's current time zone

T
TheMengzor, 2012-07-20
@TheMengzor

Store on the server in UTC, and on the client:
var timezoneDate = new Date();
var timezoneOffset = timezoneDate.getTimezoneOffset();
var date = new Date((1342382400 - (timezoneOffset * 60)) * 1000);
console log(date);
From the timestamp in seconds, subtract the timezone shift (in minutes * 60) in seconds, then multiply by the mower and make happiness :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question