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中BLOB字段的方法示例【存储文本与图片】

本文实例讲述了PHP操作MySQL中BLOB字段的方法。分享给大家供大家参考,具体如下: 1、MySQL中BLOB字段类型 BLOB类型的字段用于存储二进制数据。 MySQL中,BLOB...

php将textarea数据提交到mysql出现很多空格的解决方法

本文实例讲述了php将textarea数据提交到mysql出现很多空格的解决方法。分享给大家供大家参考。具体分析如下: 有一些朋友可能会发现我们在html提交给php处理保存数据到mys...

MySQL GBK→UTF-8编码转换

前言: 第一次写教程,其实算不得教程,只是想总结个转换的手记。如果中间有错误,或者办法不够理想,大家回贴研究下。 另外,我也希望我们论坛不仅仅作为闲聊的地方,也希望大家能活跃我们论坛的学...

php+mysqli预处理技术实现添加、修改及删除多条数据的方法

本文实例讲述了php+mysqli预处理技术实现添加、修改及删除多条数据的方法。分享给大家供大家参考。具体分析如下: 首先来说说为什么要有预处理(预编译)技术?举个例子:假设要向数据库添...

五款常用mysql slow log分析工具的比较分析

五款常用mysql slow log分析工具的比较分析

启用 slow log 有两种启用方式:1, 在my.cnf 里 通过 log-slow-queries[=file_name] 2, 在mysqld进程启动时,指定--log-sl...