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');
Related
I have a message table schema like this (msg_id, msg_from_id, msg_to_id, msg_text, msg_time).
...and I'm thinking about the conversion of the message timestamp.
Store the user's timezone in the users table, do an AT TIME ZONE = 'SELECT tz FROM users where ...' while retrieving the messages.
Retrieve the data just as is and convert on the client side
What's the better approach between the two?
The simple and correct solution is to set the database parameter timezone correctly for the client session:
SET timezone = 'Asia/Kolkata';
Then the database will render all timestamp with time zone correctly for that time zone. In short, PostgreSQL can do the time zone management for you.
In my application there is a requirement to display date times in different time zones. All these timestamps are stored in the database in UTC.
If I send the UTC date time to client and display using JavaScript, it automatically converts it to the client's time zone.
For example:
//the string is what I receive from the server
var date = new Date('2019-05-03T09:30:00.000+05:30');
document.write(date.toLocaleString("en-US"));
displays 5/2/2019, 11:00:00 PM in the browser which is in the Central Time Zone.
What if I do not want this automatic change, and just want to display the date and time that I received from the server?
I know I can get the offset from the date object, but then I have to do additional processing to display the same value that I already got from the server!
I am using NodeJS/javascript to determine the current date which has the time zone of the system. Now i want to insert this date into MySQL with UTC+0. After that I want to select this date with NodeJS/javascript from MySQL again with UTC+0 and insert it now into MS SQL where I want it to be stored again as UTC+0. At the end I select with NodeJS/javascript again, but this time from the MS SQL and also I want the date this time to be in the timezone which is used right now in the system wherever I am in the future.
Here's an example:
For 01.01.2018 15:00:00 UTC+1(German winter time) I want it to be stored in both databases with UTC+0. When summer comes and the time zone of my system changes to UTC+2(German summer time) or I move to another country with e.g. UTC-5 I want to get the date from MS SQL to be converted into the time zone my system uses automaticly.
So the questions are:
How can I insert my date from NodeJS with the systems time zone to MySQL with UTC+0?
How can I select this date, so that I can insert it with NodeJS into the MS SL and it's UTC+0 there too?
How can I select the date from MS SQL with NodeJS so that it has the systems time zone again?
Additional info:
I have two programs in NodeJS communicating with each other. the first one (client) is inserting a date into his MySQL database and sends this to date also to the server application. the reason why he's inserting it into his own MySQL database is for synchronisation reasons when the server is not running, to get them another time. When the server receives the dates from the clients, those should be stored in the servers MS SQL database. admins of the server are able to export those dates into Excel whenever they want. but the dates have to be converted from the date in the MS SQL (UTC+0) into the servers system time zone, so that in the excel file the date is in the systems time zone instead of UTC+0
I think there are some optimizations which can be made across this solution, but I'll answer your questions first.
How can I insert my date from NodeJS with the systems time zone to MySQL with UTC+0?
You get the system time zone using new Date() - to convert that to UTC format which MySQL would understand you can do new Date().toISOString()
To store this value in MySQL you need a DateTime field. Just inserting that value should work, but if it doesn't just parse it for it to be a date.
How can I select this date, so that I can insert it with NodeJS into
the MS SQL and it's UTC+0 there too?
Well you've inserted it in a standard format which is always UTC+0. You just select it and insert it the same.
How can I select the date from MS SQL with NodeJS so that it has the
systems time zone again?
So - you have a DateTime field in MS SQL which you'll be querying through Node.JS - the field is in UTC+0 and you want to get that time accordingly for your server's timezone?
Well - if you do new Date().getTimezoneOffset() that should give you your timezone offset. So it should become something like this:
var date = MSSQLWrapper.someOperation('select d_field from ...');
var offset = new Date().getTimezoneOffset(); // Your offset
date.setHours(date.getHours() + offset); // Add your offset
console.log(date) // your aligned date.
Edit
If you want to change the timezone on selection from SQL you can use AT TIME ZONE
SELECT your_date from your_table
AT TIME ZONE 'Eastern Standard Time' AS my_new_date
You can pass the timezone name in your query from your server side.
or alternatively you can check out TODATETIMEOFFSET
This question also has some good examples: Convert datetime value from one timezone to UTC timezone using sql query
Use moment js and its UTC feature.
This way you will not need to handle the timezones.
Greetings
Dominik
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.
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.