How do I change MySQL timezone?

2015-01-24信息快讯网

The MySQL timezone is set to MST (-7 hours GMT/UTC) and is not configurable by you. MySQL is only capable of having 1 timezone setting per mysql daemon. Therefore, you cannot select NOW() and expect a result in a timezone other than MST.



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.
Search File Contents PHP 搜索目录文本内容的代码
php microtime获取浮点的时间戳
GBK的页面输出JSON格式的php函数
同台服务器使用缓存APC效率高于Memcached的演示代码
Memcache 在PHP中的使用技巧
PHP Memcached应用实现代码
Zend framework处理一个http请求的流程分析
PHP has encountered an Access Violation 错误的解决方法
PHP strtotime函数详解
PHP下编码转换函数mb_convert_encoding与iconv的使用说明
Php 构造函数construct的前下划线是双的_
php session_start()关于Cannot send session cache limiter - headers already sent错误解决方法
php self,$this,const,static,->的使用
php面向对象全攻略 (十) final static const关键字的使用
PHP has encountered an Access Violation at 7C94BD02解决方法
PHP setcookie() cannot modify header information 的解决方法
PHP Google的translate API代码
mysql时区问题
有关 PHP 和 MySQL 时区的一点总结
使用 MySQL Date/Time 类型
php模板之Phpbean的目录结构
Phpbean路由转发的php代码
php框架Phpbean说明
优化使用mysql存储session的php代码
EPSON打印机 连供墨水系统 维修有哪些保养窍门第1/2页
利用static实现表格的颜色隔行显示的代码
wordpress之wp-settings.php
关于在php.ini中添加extension=php_mysqli.dll指令的说明
解决php中Cannot send session cache limiter 的问题的方法
Linux下ZendOptimizer的安装与配置方法
PHP+Tidy-完美的XHTML纠错+过滤
理解PHP5中static和const关键字的区别
PHP 中dirname(_file_)讲解
PHP编码规范-php coding standard
六酷社区论坛HOME页清新格调免费版 下载
©2014-2024 dbsqp.com