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

更新

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

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

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


当前回答

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).

其他回答

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

SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP);

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

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

可能是这样

select timediff(current_time(),utc_time())

通过这种方式,您不会直接获得时区值。

@@global。time_zone不能被使用,因为它是一个变量,它返回的值是'SYSTEM'。

如果您需要使用session SET TIME_ZONE =在更改了时区的会话中使用您的查询,那么您将使用@@session.time_zone获得该查询。如果查询@@global。time_zone,然后你得到'SYSTEM'。

如果使用now()和utc_time()尝试datediff、date_sub或timediff,那么可能会遇到转换问题。

但是上面建议的东西至少可以在某些服务器版本上使用。我的版本是5.5.43-37,是一个托管解决方案。

要获取mysql的当前时区,你可以做以下事情:

 SELECT @@system_time_zone;   # from this you can get the system timezone 
 SELECT IF(@@session.time_zone = 'SYSTEM', @@system_time_zone, @@session.time_zone) # This will give you time zone if system timezone is different from global timezone

现在如果你想改变mysql时区,那么:

 SET GLOBAL time_zone = '+00:00';   # this will set mysql timezone in UTC
 SET @@session.time_zone = "+00:00";  # by this you can chnage the timezone only for your particular session 

简单的 选择@@system_time_zone;

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

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

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

尝试使用以下代码:

//ASP CLASSIC
Set dbdate = Server.CreateObject("ADODB.Recordset")
dbdate.ActiveConnection = MM_connection
dbdate.Source = "SELECT NOW() AS currentserverdate "
dbdate.Open()
currentdate = dbdate.Fields("currentserverdate").Value
response.Write("Server Time is "&currentdate)
dbdate.Close()
Set dbdate = Nothing