How do I change MySQL timezone?

yipeiwu_com6年前Mysql基础


However, there are ways for you to get results that are in your preferred timezone. First determine how many hours your desired timezone is off from MST. For example, EST is +2 hours. PST is -1 hour.

Knowing the time offset, you can replace all your SQL statements of 


SELECT NOW();

with


SELECT DATE_ADD(NOW(), INTERVAL 2 HOUR);

which will give you an EST date result. For a result in PST, you would do:


SELECT DATE_SUB(NOW(), INTERVAL 1 HOUR);

If you are working with time in seconds instead of dates, then factor in the offset in seconds. Because there are 3600 seconds in an hour, and EST is 2 hours later than MST, the following converts timestamps from MST to EST:


SELECT unix_timestamp() + (3600 * 2);

SELECT FROM_UNIXTIME(UNIX_TIMESTAMP() + (3600 * 2));

See the MySQL Manual's Date and Time Functions for more information.

Depending on your application, you may also need to do one of the following (but not both):

1. Find every place in your code where a date or time is displayed to the browser and have a user defined function change it to add or subtract the appropriate number of hours before displaying it.

2. Find every place in your code where dates or times are input into your system and have a user defined function add or subtract the appropriate number of hours before storing it.

相关文章

php连接mysql数据库最简单的实现方法

在连接MySQL数据库之前,您必须指定以下信息: MySQL数据源名称或DSN:指定MySQL数据库服务器的地址。您可以使用IP地址或服务器名称,例如,127.0.0.1 或 local...

php监测数据是否成功插入到Mysql数据库的方法

前言 本文主要介绍的是php代码中监测数据是否成功插入到Mysql数据库,可以使用这两种方式。下面话不多说,来看看详细的解决方法。 解决方法 第一种就是通过mysql_query()函...

关于mysql字符集设置了character_set_client=binary 在gbk情况下会出现表描述是乱码的情况

mysql链接建立之后,通过如下方式设置编码: 复制代码 代码如下: mysql_query("SET character_set_connection=" . $GLOBALS['ch...

php+mysql实现无限级分类

项目思路分析:一个PHP项目要用到分类,但不确定分几级,所以就想做成无限级分类。 一开始想是按以前一样,数据库建4个值,如下: id: 自增   | &nb...

php写入mysql中文乱码的实例解决方法

php写入mysql出现中文乱码的解决办法是:在建立数据库连接之后,将该连接的编码方式改为中文。 代码如下: $linkID=@mysql_connect("localhost","...