Is there any timezone attached with datetime in MySQL? - mysql

Is there any timezone attached (like metadata) with every datetime field values in MySQL? Or every dates are independent of timezone?

Is there any timezone attached (like metadata) with every datetime field values in MySQL?
No, there is no timezone stored in DB with every date (like metadata). MySQL, by default, takes the timezone of the server for all stored dates.
To find out what it is try to run this in the shell
mysql> SELECT ##global.time_zone, ##session.time_zone;
Now as you can see there are always these 2 timezones for the serve
Global(for all mysql connections)
Session - local to the connection
Go through this Stackoverflow question to read more about this.
Also this question to read how to change/set it

Related

Mysql data insert issue in timestamp type

Using DBeaver we are trying to execute the following query.
UPDATE listing SET ScheduledTime='2019-01-09 15:14:51.0', Status='SCHEDULED' where ID=108
after successful execution, we can see the ScheduledTime column as '2019-01-09 20:44:51' in DB. Why there is a time mismatch and how we can solve it? Assistance in this matter is greatly appreciated.
You use different timezone setting when you store and when you view the data. As mysql documentation on timestamp says:
MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.) By default, the current time zone for each connection is the server's time. The time zone can be set on a per-connection basis.
Mysql documentation describes how to view and set timezone here

Update my existing datetime column to my respective timezone in mysql

I have a table named "students" where student information are stored. Last week, I added a column (type-datetime) to keep track students last login time.
It was working well when testing with localhost. So, I uploaded to hosting and after a few days, I noticed datetime are different with my local times. I called now() function in my code and it is inserting with server time (Seattle,USA Time). I tried to set it with my timezone.
SELECT ##session.time_zone;
SET time_zone = 'Asia/Rangoon';
SET time_zone = "+06:30";
SET ##session.time_zone = "+06:30";
although it is executed, it don't affect and inserting with server time like before.
My Question is how should I update my existing datetime value column to my respective timezone. Thanks and appreciating.
The best approach imho is to save all dates in UTC. Only at presentation time these times should be converted to the time and zone for the user.
Because you are using PHP, you can convert input into UTC and convert output to the desired timezone for presentation.
This makes it also easy and possible to show foreign visitors their own time on your website.

Stop changing dates automatically in MySQL on tz change

I am using DATETIME field to store my dates in MySQL. Without any timezone specified directly (there is just a datetime column)
But when timezone was changed on server MySQL updated all datetime columns according to new timezone.
Actually switching to EDT was the reason.
I don't need to recalculate my dates automatically - just want to store specific dates in it.
So even if tz changed manually to UTC from EST date should be same (from characters POV) if it was 2016-01-01 18:55 it should be same in any new tz..
I did not run any scripts\queries to update dates.
So it was performed either by MySQL itself or by server.
Need advice what I need to check to find and disable such feature.
Make sure you are using DATETIME and not TIMESTAMP
[From the MySQL documentation][1]:
MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.)
You should also review this post (Duplicate):
Will changing a MySQL timezone change values of DateTime fields in a database?
I am sorry for the mess I brought
Just extended my query to grab some old dates - and it looks unchanged
So error defenetly in my code..
Anyway - thanks for your help

MySQL timezone discrepancy on Update tale

I'm working on a shared hosting so I don't have access to mysql configuration files.
The default timezone on that system is 'America/New_York' but I need to use UTC -5:00 for my databases.
Everytime I perform a query the timezone is set to UTC -5:00 like this:
SET time_zone='-5:00';
If I want the current time, SELECT NOW() returns the correct time and date, but when updating a table mysql uses the SYSTEM time and not the set timezone
UPDATE administradores SET ultimo_acceso=NOW() WHERE id=1
Why does those values are different? Shouldn't be the same time in both queries since I'm overriding the timezone?
I also tried with INSERT statement and that works fine.
If the ultimo_acceso field is of type TIMESTAMP, then the value is actually being stored as UTC and then converted back to the current time zone when you select the value back out. So you need to set the time zone again in the select statement.
If you are using a DATETIME data type, then the value you set should be persisted without conversion and you will get back exactly what you store - regardless of the timezone setting at time of select.
See the MySQL docs on this subject.
Note that DATETIME, DATE, and TIME fields will always return the exact time you placed into them, regardless of the value of the time_zone variable.
TIMESTAMP fields will automatically convert their values to the timezone specified by the time_zone variable.

How does mySQL determine the time zone for the TIMESTAMP field?

According to mysqltutorial
The values of the MySQL TIMESTAMP columns depend on connection’s time
zone. When insert values for MySQL TIMESTAMP columns, they are
converted to Universal Coordinated Time (UTC) from connection’s time
zone. When you select the value, the server converts it back from UTC
to the connection’s time zone so you have the same value that you
inserted.
Where does it get this info. from, the time the user sets his OS to or from some other method?
Javascript also has a way to do timezones but is more involved: onlineaspect
To specify connection-specific time you need to perform
SET time_zone = TZ;
Where TZ can be either numerical offset like +11:00 or name of timezone Asia/Vladivostok (for the latter you need to import timezones description. Ask your DBA to do so)
the example there is wrong, i just tested it. it returns the previously inserted timestamp, regardless of the timezone. mysql will always insert the time you specify, without modifying it. if you need timezone adjustment, you have to tell mysql explicit to do so; otherwise all times are assumed to be "local server time".