如何获取MySQL的当前时区? [英] How do I get the current time zone of MySQL?

查看:108
本文介绍了如何获取MySQL的当前时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道MySQL中是否有这样的功能吗?

Anyone knows if there is such a function in MySQL?

更新

这不会输出任何有效信息:

This doesn't output any valid info:

mysql> SELECT @@global.time_zone, @@session.time_zone;
+--------------------+---------------------+
| @@global.time_zone | @@session.time_zone |
+--------------------+---------------------+
| SYSTEM             | SYSTEM              |
+--------------------+---------------------+

或者MySQL本身可能不完全知道所使用的time_zone,那很好,我们可以在这里使用PHP,只要我能获得不像SYSTEM的有效信息...

Or maybe MySQL itself can't know exactly the time_zone used,that's fine, we can involve PHP here, as long as I can get valid info not like SYSTEM...

推荐答案

摘自手册(

可以像这样检索全局和特定于客户的时区的当前值:
mysql> SELECT @@global.time_zone, @@session.time_zone;

The current values of the global and client-specific time zones can be retrieved like this:
mysql> SELECT @@global.time_zone, @@session.time_zone;

编辑如果将MySQL设置为系统时区的从属,则以上返回SYSTEM,对您的帮助不大.由于您使用的是PHP,如果MySQL的答案为SYSTEM,则可以通过

Edit The above returns SYSTEM if MySQL is set to slave to the system's timezone, which is less than helpful. Since you're using PHP, if the answer from MySQL is SYSTEM, you can then ask the system what timezone it's using via date_default_timezone_get. (Of course, as VolkerK pointed out, PHP may be running on a different server, but as assumptions go, assuming the web server and the DB server it's talking to are set to [if not actually in] the same timezone isn't a huge leap.) But beware that (as with MySQL), you can set the timezone that PHP uses (date_default_timezone_set), which means it may report a different value than the OS is using. If you're in control of the PHP code, you should know whether you're doing that and be okay.

但是关于MySQL服务器使用哪个时区的整个问题可能是切线的,因为询问服务器它所在的时区并不会告诉您有关数据库中数据的绝对没有.继续阅读以获取详细信息:

But the whole question of what timezone the MySQL server is using may be a tangent, because asking the server what timezone it's in tells you absolutely nothing about the data in the database. Read on for details:

进一步的讨论:

当然,如果您控制服务器,则可以确保时区为已知数量.如果您不受服务器的控制,则可以设置连接使用的时区,如下所示:

If you're in control of the server, of course you can ensure that the timezone is a known quantity. If you're not in control of the server, you can set the timezone used by your connection like this:

set time_zone = '+00:00';

将时区设置为GMT,以便任何进一步的操作(如now())都将使用GMT.

That sets the timezone to GMT, so that any further operations (like now()) will use GMT.

不过请注意,时间和日期值与时区信息一起不存储在MySQL中:

Note, though, that time and date values are not stored with timezone information in MySQL:

mysql> create table foo (tstamp datetime) Engine=MyISAM;
Query OK, 0 rows affected (0.06 sec)

mysql> insert into foo (tstamp) values (now());
Query OK, 1 row affected (0.00 sec)

mysql> set time_zone = '+01:00';
Query OK, 0 rows affected (0.00 sec)

mysql> select tstamp from foo;
+---------------------+
| tstamp              |
+---------------------+
| 2010-05-29 08:31:59 |
+---------------------+
1 row in set (0.00 sec)

mysql> set time_zone = '+02:00';
Query OK, 0 rows affected (0.00 sec)

mysql> select tstamp from foo;
+---------------------+
| tstamp              |
+---------------------+
| 2010-05-29 08:31:59 |      <== Note, no change!
+---------------------+
1 row in set (0.00 sec)

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2010-05-29 10:32:32 |
+---------------------+
1 row in set (0.00 sec)

mysql> set time_zone = '+00:00';
Query OK, 0 rows affected (0.00 sec)

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2010-05-29 08:32:38 |      <== Note, it changed!
+---------------------+
1 row in set (0.00 sec)

因此,了解服务器的时区仅对于立即获取时间的功能(例如now()unix_timestamp()等)而言很重要;它不会告诉您有关数据库数据中的日期正在使用哪个时区的任何信息.您可能会选择假设,它们是使用服务器的时区编写的,但是这种假设很可能是有缺陷的.要了解数据中存储的任何日期或时间的时区,您必须确保它们与时区信息一起存储,或者(就像我一样)确保它们始终位于格林尼治标准时间.

So knowing the timezone of the server is only important in terms of functions that get the time right now, such as now(), unix_timestamp(), etc.; it doesn't tell you anything about what timezone the dates in the database data are using. You might choose to assume they were written using the server's timezone, but that assumption may well be flawed. To know the timezone of any dates or times stored in the data, you have to ensure that they're stored with timezone information or (as I do) ensure they're always in GMT.

为什么要假设使用服务器的时区写入数据有缺陷?好吧,一方面,数据可能是使用设置了不同时区的连接写入的.该数据库可能已从一台服务器移至另一台服务器,其中服务器位于不同的时区(当我继承从德克萨斯州移至加利福尼亚州的数据库时遇到了这种情况).但是即使数据已写入服务器(具有当前时区),仍然是模棱两可的.去年,在美国,夏令时于11月1日凌晨2:00关闭.假设我的服务器使用太平洋时区在加利福尼亚州,并且数据库中的值为2009-11-01 01:30:00.那是什么时候?是太平洋标准时间(太平洋标准时间)11月1日凌晨1:30,还是太平洋标准时间11月1日凌晨1:30(一个小时后)?您绝对无法知道.道德:始终将日期/时间存储在GMT中(不执行DST),并在必要时将其转换为所需的时区.

Why is assuming the data was written using the server's timezone flawed? Well, for one thing, the data may have been written using a connection that set a different timezone. The database may have been moved from one server to another, where the servers were in different timezones (I ran into that when I inherited a database that had moved from Texas to California). But even if the data is written on the server, with its current time zone, it's still ambiguous. Last year, in the United States, Daylight Savings Time was turned off at 2:00 a.m. on November 1st. Suppose my server is in California using the Pacific timezone and I have the value 2009-11-01 01:30:00 in the database. When was it? Was that 1:30 a.m. November 1st PDT, or 1:30 a.m. November 1st PST (an hour later)? You have absolutely no way of knowing. Moral: Always store dates/times in GMT (which doesn't do DST) and convert to the desired timezone as/when necessary.

这篇关于如何获取MySQL的当前时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆