How to change database timezone from UTC to local in wordpress - mysql

I'm using a custom plugin in wordpress. All date and time are stored in database as UTC. I saw in posts table, there're 2 columns for UTC and local timezone (post_date, post_date_gtm), but in this plugin table, there's only 1 column to store the created date as UTC.
Currently, I'm using sql query and $wpdb to show the data between "start date" and "end date".
Please help me to show them in local timezone (That is setted in general setting) instead of UTC!
Thanks and sorry about my English.

Assume your local time zone is +05:00, use the following condition:
WHERE CONVERT_TZ(col_date_utc, '+00:00', '+05:00') BETWEEN '....' AND '....'

I thought this might be useful
There are 3 places where the timezone might be set in MySQL:
Check here link

Have you tried Admin> Settings> Timezone?

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

Convert MySQL times tamp column from one timezone to another

Need some ideas on how to convert an entire database TIMESTAMP columns from one timezone to another.
The current server is UTC, MySQL is also UTC so everything is good in that area. All time related columns are TIMESTAMPs. The problem is that when the time information was being entered, they were in EST/EDT. For example, enter start time: data is 1/1/2011 08:00:00 AM (EST/EDT). Because timezone wasn't implemented at the start, the database stored this as 08:00:00 UTC. So all the data in the database is stored like this. Once we get data that requires timezone info, this model will break.
The question is: how do you convert all these TIMESTAMP columns into the correct UTC time? The code will be changed to deal with this on the display side on a go-forward basis but what about historic data?
The simplest way seems to do a mysqldump --tz-utc of some sort and then import the data back, then release the code. However, I can't seem to find a good example of how to do this properly or if there are other ways to do this in the most efficient way possible.
Thanks!
Could you use the MySQL AddTime function to update the existing data?
UPDATE MyTable SET MyTimeColumn = ADDTIME (MyTimeColumn, -8:00:00) WHERE <the data is bad>

How to set MySQL to use GMT in Windows and Linux

I'm just trying to get MySQL to store time in GMT...
I've read the documentation here: http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html
It says to set: default-time-zone='timezone' in an option file.
However, I've googled for several different terms and cannot find possible values "timezone" is supposed to be. I also don't know where in my.ini (and in Linux, my.cnf) to put this directive. Below [msyqld] ?
Go to [mysqld] section of your my.ini file and under that section enter this line
default-time-zone = '+00:00'
'+00:00' indicates the offset from GMT which in your case will be 0. Please note the '+' sign in the string.
You don't need to install any timezone tables for your problem. After restarting the server, your server will operate in the UTC timezone and hence NOW() will give you the time in GMT.
By default, MySQL is set to your SYSTEM timezone ie. your server timezone is same as your system timezone. So you could also change your system time zone to solve your problem, though this is not recommendable.
Just to save a few clicks, the list of zone names is shown like this:
select name from mysql.time_zone_name;
Beware of setting a fixed-offset zone such as 'GMT' or '+00:00' since it will not alter to match local time / DST etc. If you want it to follow local time, set the zone to 'Europe/London' instead. If you don't want that I'd go for UTC over GMT anyway.
The MySQL Timezone table is not loaded by default, which might be why you're experiencing difficulties. You need to load the timezone table and then set your timezone using the instructions above.
I use this for GMT+2:
SET GLOBAL time_zone = '+02:00';
Screw this... DATETIME, although much easier on the eye when viewing tables, isn't worth this nightmare.
I'm just going to use TIMESTAMP.
Yes, that's a server option for mysqld, and so should be put in the [mysqld] section.
Also, regarding values, see The Definitive Guide to MySQL 5