Home  >  Article  >  Database  >  How to set MySQL time zone

How to set MySQL time zone

jacklove
jackloveOriginal
2018-06-08 10:27:133054browse

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)

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

under [mysqld]. For example:

default-time-zone = ‘+8:00’

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’;

>>In the PHP program>>

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

>>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('时区设置失败,请联系管理员!');

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!

Statement:
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
Previous article:MYSQL query time dataNext article:MYSQL query time data