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.

相关文章

Zend studio for eclipse中使php可以调用mysql相关函数的设置方法

如果直接调用mysql_connect会出现:Fatal error: Call to undefined function mysql_connect() 的错误 解决办法, 找到ze...

win7+apache+php+mysql环境配置操作详解

1.php版本简介php各版本之间的区别,php版本后面一般有VC6和VC9、Thread Safe和Non Thread Safe的区别,VC6就是legacy Visual Stud...

PHP读MYSQL中文乱码的快速解决方法

打算切换某个网站的主机,没想到遇到Php和Mysql中文乱码的问题。 以前的国外主机用的Mysql是4.x系列的,感觉还比较好,都无论GBK和UTF-8都没有乱码,没想到新的主机的Mys...

php中mysql连接方式PDO使用详解

PDO常用方法: PDO::query()主要用于有记录结果返回的操作(PDOStatement),特别是select操作。 PDO::exec()主要是针对没有结果集合返回的操作。如...

使用PHP实现Mysql读写分离

本代码是从uchome的代码修改的,是因为要解决uchome的效率而处理的。这个思维其实很久就有了,只是一直没有去做,相信也有人有同样的想法,如果有类似的,那真的希望提出相关的建议。封装...