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、mysqli、pdo】

本文实例讲述了PHP连接MySQL数据库的三种方式。分享给大家供大家参考,具体如下: PHP与MySQL的连接有三种API接口,分别是:PHP的MySQL扩展 、PHP的mysqli扩展...

PHP实现清除MySQL死连接的方法

本文实例讲述了PHP实现清除MySQL死连接的方法。分享给大家供大家参考,具体如下: 连接的情况,主要表现为有过多的Sleep连接,并且Time时间很长,占满了所有的可用连接数,以至于其...

PHP用mysql_insert_id()函数获得刚插入数据或当前发布文章的ID

前言 最近在工作中又遇到了这个问题,PHP中如何获得刚插入数据的ID(或当前发布文章的ID)呢?觉得有必要整理下详细的解决方法,方便自己也给有需要的朋友们提供以帮助,那么话不多说了,来看...

在mysql数据库原有字段后增加新内容

复制代码 代码如下:update table set user=concat(user,$user) where xx=xxx;...

mysql From_unixtime及UNIX_TIMESTAMP及DATE_FORMAT日期函数

from_unixtime()是MySQL里的时间函数 date为需要处理的参数(该参数是Unix 时间戳),可以是字段名,也可以直接是Unix 时间戳字符串 后面的 '%Y%m%d'...