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.
When you always send the UTC datetime to your server and let javascript convert it back when showing, the problem will not occur.
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
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
Setting the correct timezone information in the php.ini file did the trick.
Thanks.
Related
The default timezone in the laravel application is UTC and I'm creating a chat application that needs to show the user the time they are sending and receiving messages. Javascript seems to be very easy to convert the database time to the user's local timezone, I used the moment.js library to do this conversion in Javascript but now I don't want to use Javascript I want to use PHP to do the conversion. The problem with using PHP is that it is not converting the time to the actual user timezone, for example, my timezone is UTC+1 but I can only use UTC in PHP.
This is what the Javascript code looks like:
let time = moment.utc(data.created_at).local().format('YYYY-MM-DD HH:mm:ss');
And this is what the PHP code looks like:
{{date('H:i', strtotime($data->created_at))}}
I want to convert the database time to the user's local time using the user's timezone. The Javascript code works fine but the PHP is not working fine. For the UTC+1 the PHP code is an hour slow.
Don't rely on strtotime() or similar methods that "magically" know what format your date is. They guess. Educated guesses, but still guesses.
Use PHP's family of DateTime classes that are properly aware of timezones without mucking about with system-level settings.
$in_str = '2022-07-13 12:34:56';
$in_date = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $in_str, new DateTimezone('UTC'));
$out_date = $in_date->setTimezone(new DateTimezone('America/Chicago'));
var_dump(
$in_date->format('c'),
$out_date->format('c')
);
Output:
string(25) "2022-07-13T12:34:56+00:00"
string(25) "2022-07-13T07:34:56-05:00"
Lastly, I think you might have it backwards. You should be formatting the date/time for human eyeballs as the last thing before displaying it, eg: in Javascript. Everything for backend computation/storage [eg: PHP stuff] should ideally be in a single timezone, and that timezone should ideally be UTC for consistency's sake.
Javascript also has the benefit of running in the browser where it can access the user's timezone preference which is notably not sent in HTTP requests by default. If you want PHP to know about it, you'll have to write some Javascript to send it anyways.
I need to change my timezone to India timezone, but my shared server database showing some junk timezone I tried all the possibility but I can't get the India time zone.
I am using PHP Codeigniter
I Have a contact us page on my website, which saves all the send inquiry details to my mail and database. on that is showing some junk time on it
please help me out of this issue
In the case of shared hosting you would need to contact support guys or go for a live chat and they can change the default time zone for you.
You can use date_default_timezone_set() which sets a default timezone.
Please give a look here
OR
convert all your times to UTC on server side and just work with UTC instead
EDITED ANSWER
What you can do is :
Store your date time in DB in UTC, and while showing You can show Date time By getting current timezone of India.
// create a $dt object with the UTC timezone
$dt = new DateTime('2016-12-12 12:12:12', new DateTimeZone('UTC'));
// change the timezone of the object without changing it's time
$dt->setTimezone(new DateTimeZone('Asia/Kolkata'));
// format the datetime
$dt->format('Y-m-d H:i:s T');
I have a DATETIME field in my table that is being displayed differently than what I'm returning.
To understand, here are the exact object fields I'm returning in my API:
However, when I'm using PostMan and looking at the results, the time is being changed from what I'm returning to this:
I don't understand what's going on. I'm trying to store the DATETIME in the database as UTC time, and then have it sent over to the client, where the client will convert it to local time.
Here's what the DATETIME dates looks like in the database:
You're not storing the dates in UTC time. "GMT-0400 (EDT)" means that they're being stored in Eastern time. The Z in the other results means UTC (or Zulu) time, according to this spec: http://www.ietf.org/rfc/rfc3339.txt
I expect that your MySQL installation has the default timezone set to the timezone that your box is in (https://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html) so when you add a time without specifying the timezone, it's assumed it's in the default timezone. That is then being converted to UTC in your response, adding four hours to the time.
You should probably specify a timezone when adding information to the database, and/or change the default timezone for your database. There may also be another layer "helpfully" changing things, depending on the API you're using to access the database, so if there is relevant information about the API, you can add that to your question.
We have server code that uses DateTime.UTCNow and is serialized as
2015-02-17T12:38:58.3220885Z
What is the correct way to interpret these serialized dates client side, taking time zones into account?
I am trying to track down a reported bug where the times appear to be incorrect and 'in the future' - at the client - I have seen no evidence of this however. The idea is for the server and client to simply be working with the same point in time.
Just do
var date = new Date('2015-02-17T12:38:58.3220885Z');
date.toString()
Will give the date according to the local time zone.
I want the user to enter an appointment date and time and send him a reminder an hour before it.
On the server I want to store the date-time in UTC, but on the client the user will enter it in his local time. Whats the best way to deal with this conversion?
Is there a plugin that natively supports this?
Yes, there's a plugin which detects the timezone according to the system-information (jsTimezoneDetect). Actually a date-object itself stores the information (but it's kinda wierdo!).
There's an interesting article and an interesting answer here on SO, which is worth a read!
Don’t do any date-time computations in javascript. You may guess the
browsers time zone to define it, but the definition itself and all
date-time computations must be made in a time-zone aware system.
As you will read this article, you'll get a deeper understanding of the problem "date/dst/utc/timezones in javascript" - therefore it would be interesting if you tell us more about your server-side:
Which framework do you use on your server-side?
Do you have any logic for represeting accounts with some settings (eg custom timezone-settings)?
The "correct" approach (which is fail-safe if you have the backend behind it) will be: let the user work with a datetime - in the browser-scope it's more or less "undefined" (no need to find the timezone, offset, or anything alike!). Now you provide this "undefined" datetime information to the server (eg sending the unix-timestamp with a hiddenfield to the server), where you have the information which actual timezone the user is assigned to. So you can now translate this "undefined" datetime-information to a time-zone aware datetime-object on the server-side and assign the information of your account-scope to it (now the "undefined" datetime-object has a local time-zone - eg "Pacific Daylight Time"). Now you can easily do any conversion you want.
The only downside of this approach is, that you will have 2 scopes of defining a timezone (OS of the user and your webapp) - but anything else is fiddling and not fail-safe!
It may be an idea to send a timestamp to your server, using the getTimezoneOffset method to calculate the UTC date/time client side. To convert it back client side you'd send the timestamp, create a dummy date client side to determine the currend timezone offset (the user may have moved to another timezone!) and create a client side date from that information. Client side you'd do something like:
//[client side send]
var d = new Date(), //now
offset = d.getTimezoneOffset()
UTCTimeStamp = d.getTime()+(60000*offset); // send to server
//[client side receive]
var offset = 60000*new Date().getTimezoneOffset(),
localDate = new Date(timestamp + (offset < 0 ? Math.abs(offset) : -offset));
Click here to do a simple test using this idea.