Home > Database > Mysql Tutorial > How to set MySQL time zone

How to set MySQL time zone

jacklove
Release: 2018-06-08 10:27:13
Original
3152 people have browsed it

The MySQL time zone defaults to the server's time zone. Often encountered in use.

You can view it through the following command

View time zone

SQL code

. mysql> show variables like '%time_zone%';
. +------------------+--------+
. | Variable_name | Value |
. +------------------+--------+
. | system_time_zone | CST |
. | time_zone | SYSTEM |
. +------------------+--------+
. 2 rows in set (0.00 sec)
Copy after login

Explanation: If the value of time_zone is SYSTEM, it means that it The time zone is the same as the time zone value of the operating system.

You can modify the time zone by modifying my.cnf

and adding

default-time-zone=timezone
Copy after login

under [mysqld]. For example:

default-time-zone = ‘+8:00’
Copy after login

Remember to restart msyql after changing it

Be sure to add it under [mysqld], otherwise the error message "unknown variable 'default-time-zone= 8:00'" will appear

In addition, you can also pass the command set time_zone = timezone

For example, Beijing time (GMT 0800)

set time_zone = ‘+8:00’;
Copy after login

>>In the PHP program>>

date_default_timezone_set('PRC') or die('时区设置失败,请联系管理员!');
Copy after login

>>When querying or using the database>>

//Once the database is connected, the code to change the MySQL time follows.

mysql_query("SET time_zone = '+8:00'") or die('时区设置失败,请联系管理员!');
Copy after login

This article explains how to set the MySQL time zone. For more related content, please pay attention to the php Chinese website.

Related recommendations:

MYSQL query time data

How to use database methods to save session

Five ways to prevent SQL injection

The above is the detailed content of How to set MySQL time zone. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template