如何在 MySql 选择查询中将 UTC 日期转换为本地时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15017799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:40:00  来源:igfitidea点击:

How to Convert UTC Date To Local time Zone in MySql Select Query

mysqlutclocaltimeconvert-tz

提问by User 1531343

I am using this Where Condition in One Of my query with MySql Database.My Problem is that i have one displaytime column in my table but that table column shows the data in UTC Time.and i want to convert that displaytime column in the Local Time Zone.so how can i provide this facility from query itself.

我在 MySql 数据库的其中一个查询中使用了这个 Where 条件。我的问题是我的表中有一个显示时间列,但该表列以 UTC 时间显示数据。我想在本地时间中转换该显示时间列Zone.so 如何从查询本身提供此功能。

I have goggled the things and by that i knew that something like SELECT CONVERT_TZ()will work for that.but its not working for me.

我已经对这些东西进行了检查,因此我知道类似的东西SELECT CONVERT_TZ()会为此起作用。但它对我不起作用。

Here is my query in which i need to convert displaytime to local time zone...so can anyone please guide me?

这是我的查询,我需要将显示时间转换为本地时区...所以有人可以指导我吗?

WHERE displaytime >= '2012-12-01 00:00:00'
  AND displaytime <='2013-02-22 23:59:59'
  AND ct.organizationId IN (
    SELECT t.organizationId
      FROM organization_ AS t
      JOIN organization_ AS p ON t.treePath LIKE CONCAT(p.treePath, '%')
     WHERE p.organizationId = 10707

enter image description hereSAmple DAta

在此处输入图片说明样本数据

enter image description here

在此处输入图片说明

回答by iiro

SELECT CONVERT_TZ() will work for that.but its not working for me.

SELECT CONVERT_TZ() 将为此工作。但它对我不起作用。

Why, what error do you get?

为什么,你得到什么错误?

SELECT CONVERT_TZ(displaytime,'GMT','MET');

should work if your column type is timestamp, or date

如果您的列类型是时间戳或日期,则应该有效

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_convert-tz

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_convert-tz

Test how this works:

测试它是如何工作的:

SELECT CONVERT_TZ(a_ad_display.displaytime,'+00:00','+04:00');

Check your timezone-table

检查您的时区表

SELECT * FROM mysql.time_zone;
SELECT * FROM mysql.time_zone_name;

http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html

http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html

If those tables are empty, you have not initialized your timezone tables. According to link above you can use mysql_tzinfo_to_sqlprogram to load the Time Zone Tables. Please try this

如果这些表为空,则说明您尚未初始化时区表。根据上面的链接,您可以使用mysql_tzinfo_to_sql程序加载时区表。请试试这个

shell> mysql_tzinfo_to_sql /usr/share/zoneinfo

or if not working read more: http://dev.mysql.com/doc/refman/5.5/en/mysql-tzinfo-to-sql.html

或者如果不工作阅读更多:http: //dev.mysql.com/doc/refman/5.5/en/mysql-tzinfo-to-sql.html

回答by Felix Geenen

In my case, where the timezones are not available on the server, this works great:

在我的情况下,服务器上没有时区,这很好用:

SELECT CONVERT_TZ(`date_field`,'+00:00',@@global.time_zone) FROM `table`

Note: global.time_zone uses the server timezone. You have to make sure, that it has the desired timezone!

注意: global.time_zone 使用服务器时区。您必须确保它具有所需的时区!

回答by Som

 select convert_tz(now(),@@session.time_zone,'+05:30')

replace '+05:30' with desired timezone. see here - https://stackoverflow.com/a/3984412/2359994

用所需的时区替换“+05:30”。见这里 - https://stackoverflow.com/a/3984412/2359994

to format into desired time format, eg:

格式化为所需的时间格式,例如:

 select DATE_FORMAT(convert_tz(now(),@@session.time_zone,'+05:30') ,'%b %d %Y %h:%i:%s %p') 

you will get similar to this -> Dec 17 2014 10:39:56 AM

你会得到类似的结果 -> 2014 年 12 月 17 日上午 10:39:56