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

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)

当前回答

如果你使用PDO:

$offset="+10:00";
$db->exec("SET time_zone='".$offset."';");

如果你正在使用MySQLi:

$db->MySQLi->query("SET time_zone='".$offset."';");

关于格式化偏移量的更多信息: https://www.sitepoint.com/synchronize-php-mysql-timezone-configuration/

其他回答

首先要弄清楚time_zone是什么,可以查询

SHOW VARIABLES LIKE '%time_zone%'; 

您的输出应该如下所示

**Variable_name**     **Value**
system_time_zone      CDT
time_zone             SYSTEM

然后,如果你想确认你所在的时区,比如CDT,而不是EST,你可以通过这样说来检查它认为你的机器所在的时间

SELECT NOW();

如果这不是你想要的时间,你需要改变它……你所需要做的就是SET time_zone = timezone_name。确保它是一个大洲/城市格式。

如果您在共享服务器上,因为您有托管服务,请参考这些关于更改php.ini文件或.htaccess文件的答案。

要在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表!

简单地在你的MySQL服务器上运行:

SET GLOBAL time_zone = '+8:00';

你所在的时区是+8:00。

您必须设置您的位置时区。接下来就是下面的过程 打开您的MSQLWorkbench 像这样写一个简单的SQL命令;

select now();

url也可以是这样的;

url = "jdbc:mysql://localhost:3306/your_database_name?serverTimezone=UTC";

在我的例子中,解决方案是在高级设置中将serverTimezone参数设置为适当的值(CET表示我的时区)。

当我使用IntelliJ时,我使用它的数据库模块。当添加一个新的连接到数据库,并在“常规”选项卡中添加所有相关参数后,“测试连接”按钮上出现错误。同样,解决方案是在高级选项卡中设置serverTimezone参数。