在一台服务器上,当我运行:

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2009-05-30 16:54:29 |
+---------------------+
1 row in set (0.00 sec)

在另一台服务器上:

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2009-05-30 20:01:43 |
+---------------------+
1 row in set (0.00 sec)

当前回答

MySQL和PHP的时区设置:

记住:

更改时区系统。Ubuntu的例子: $ sudo dpkg-reconfigure tzdata 重启服务器或者重启Apache 2和MySQL: /etc/init.d / mysql重启

其他回答

要在MariaDB中设置标准时区,必须转到50-server.cnf文件。

sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

然后您可以在mysqld部分中输入以下条目。

default-time-zone='+01:00'

例子:

#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#

# this is read by the standalone daemon and embedded servers
[server]

# this is only for the mysqld standalone daemon
[mysqld]

#
# * Basic Settings
#
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking

### Default timezone ###
default-time-zone='+01:00'

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.

必须通过配置文件进行更改,否则MariaDB服务器重启后将重置mysql表!

要为当前会话设置它,请执行:

SET time_zone = timezonename;

您可以在启动服务器时指定服务器的默认时区,请参阅http://dev.mysql.com/doc/refman/5.1/en/server-options.html,特别是——default-time-zone=timezone选项。可以查看全局时区和会话时区

SELECT @@global.time_zone, @@session.time_zone;

使用set语句设置其中一个或两个,更多细节请参见http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html。

我觉得这个可能有用:

MySQL中有三个地方可以设置时区:

在[mysqld]部分的“my.cnf”文件中

default-time-zone='+00:00'

@@global。time_zone变量

要查看它们被设置为什么值:

SELECT @@global.time_zone;

要为它设置一个值,可以使用其中任何一个:

SET GLOBAL time_zone = '+8:00';
SET GLOBAL time_zone = 'Europe/Helsinki';
SET @@global.time_zone = '+00:00';

(使用像“欧洲/赫尔辛基”这样的命名时区意味着你必须有一个正确填充的时区表。)

记住+02:00是一个偏移量。欧洲/柏林是一个时区(有两个偏移量),CEST是一个时钟时间,对应于特定的偏移量。

@@session。time_zone变量

SELECT @@session.time_zone;

要设置它,可以使用任何一个:

SET time_zone = 'Europe/Helsinki';
SET time_zone = "+00:00";
SET @@session.time_zone = "+00:00";

两者都可能返回SYSTEM,这意味着它们使用my.cnf中设置的时区。

要使时区名称生效,必须设置需要填充的时区信息表:http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html。在这个回答中,我还提到了如何填充这些表。

获取当前时区偏移量为TIME

SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP);

如果您的时区是+2:00,它将返回02:00:00。

获取当前UNIX时间戳:

SELECT UNIX_TIMESTAMP();
SELECT UNIX_TIMESTAMP(NOW());

获取时间戳列作为UNIX时间戳

SELECT UNIX_TIMESTAMP(`timestamp`) FROM `table_name`

获取一个UTC datetime列作为UNIX时间戳

SELECT UNIX_TIMESTAMP(CONVERT_TZ(`utc_datetime`, '+00:00', @@session.time_zone)) FROM `table_name`

注意:改变时区不会改变存储的日期时间或时间戳,但它会为现有的时间戳列显示不同的日期时间,因为它们在内部存储为UTC时间戳,而在外部显示为当前MySQL时区。

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

MySQL和PHP的时区设置:

记住:

更改时区系统。Ubuntu的例子: $ sudo dpkg-reconfigure tzdata 重启服务器或者重启Apache 2和MySQL: /etc/init.d / mysql重启