有人知道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).

其他回答

将一个虚拟记录插入到具有时间戳的数据库中 选择该记录并获取时间戳的值。 删除该记录。获取服务器写入数据时使用的时区,忽略PHP时区。

描述中提到的命令返回“SYSTEM”,表示它采用服务器的时区。这对我们的查询没有用处。

下面的查询将有助于了解时区

SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP) as GMT_TIME_DIFF;

以上查询将提供与协调世界时(UTC)相关的时间间隔。所以你可以很容易地分析时区。如果数据库时区是IST,则输出将是5:30

UTC_TIMESTAMP

在MySQL中,UTC_TIMESTAMP返回当前UTC日期和时间,作为'YYYY-MM-DD HH:MM:SS'或YYYYMMDDHHMMSS的值。Uuuuuu格式取决于函数的用法,即在字符串或数字上下文中。

现在()

现在()函数。MySQL NOW()以'YYYY-MM-DD HH:MM:SS'格式或YYYYMMDDHHMMSS返回当前日期和时间的值。Uuuuuu格式,取决于函数的上下文(数字或字符串)。CURRENT_TIMESTAMP, CURRENT_TIMESTAMP(), LOCALTIME, LOCALTIME(), LOCALTIMESTAMP, LOCALTIMESTAMP()是NOW()的同义词。

尝试使用以下代码:

//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

你只需要在修改系统时区后重新启动mysqld ..

MySQL的Global timezone取System的timezone。当你改变任何这样的系统属性,你只需要重新启动Mysqld。

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

SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP);

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

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