有人知道MySQL中有没有这样的函数吗?

更新

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

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

或者MySQL本身不能确切地知道所使用的time_zone,这很好,我们可以在这里涉及PHP,只要我能得到有效的信息,而不是像SYSTEM…


当前回答

正如Jakub Vrána(创建者或管理员和NotORM)在评论中提到的,在TIME中选择当前时区偏移量使用:

SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP);

如果该日期的时区是+2:00,它将返回:02:00:00

我在这里做了一个备忘单:MySQL是否应该将它的时区设置为UTC?

其他回答

下面的查询返回当前会话的时区。

select timediff(now(),convert_tz(now(),@@session.time_zone,'+00:00'));

简单的 选择@@system_time_zone;

返回PST(或与您的系统相关的任何值)。

如果你试图确定会话时区,你可以使用这个查询: 选择如果(@@session。time_zone = 'SYSTEM', @@system_time_zone, @@session.time_zone);

如果会话时区与系统时区不同,将返回会话时区。

要根据您的时区获取当前时间,您可以使用以下命令(在我的示例中是'+5:30')

选择DATE_FORMAT (convert_tz(现在(),@@session.time_zone, ' + 05:30 '), Y % - % - % d ')

我的PHP框架使用

SET LOCAL time_zone='Whatever'

在connect之后,where 'Whatever' == date_default_timezone_get()

这不是我的解决方案,但这可以确保MySQL服务器的系统时区始终与PHP的时区相同

因此,是的,PHP强烈地参与其中,并可以影响它

Use LPAD(TIME_FORMAT(TIMEDIFF(NOW(), UTC_TIMESTAMP),’%H:%i’),6,’+') to get a value in MySQL's timezone format that you can conveniently use with CONVERT_TZ(). Note that the timezone offset you get is only valid at the moment in time where the expression is evaluated since the offset may change over time if you have daylight savings time. Yet the expression is useful together with NOW() to store the offset with the local time, which disambiguates what NOW() yields. (In DST timezones, NOW() jumps back one hour once a year, thus has some duplicate values for distinct points in time).