Reputation: 345
I need to store datetimes in a mysql database.
These datetimes come from the browser, so users in the US, Europe or Asia or wherever will see their correct times.
In the browser, i have the following javascript code:
var d = new date();
var iso_date_string = d.toISOString();
// produces something like "2017-02-17T19:42:27.100Z"
I send this string to the server which runs PHP, and in my server code I have:
$date = date( "Y-m-d H:i:s", strtotime("2014-02-17T19:42:27.100Z") );
And then I insert this value in MySQL.
Everything works fine, except I don't get the exact time, there is a 1 hour difference between what I store in MySQL and the time I have on my computer.
I think this may be related to taking into account daylight saving times, but I can't find how to do it.
Please advise.
Upvotes: 0
Views: 64
Reputation: 345
Setting the correct timezone information in the php.ini file did the trick.
Thanks.
Upvotes: 0
Reputation: 1
You can use getTimezoneOffset() to check if it's DST, because it corrects for DST.
I found this solution on http://javascript.about.com/library/bldst.htm
Upvotes: 0
Reputation: 635
The PHP date will be affected by the Timezone information embedded in the php.ini file.
As far as mysql storage is concerned, you will probably want to use UTC or some other standard not affected by daylight savings to store your datetime in mysql. Then, regardless of which browser reads it, it should get converted back according to their local timezone information.
See this article for further reading:
Daylight saving time and time zone best practices
Upvotes: 0
Reputation: 296
When you always send the UTC datetime to your server and let javascript convert it back when showing, the problem will not occur.
Upvotes: 1