当前位置: 首页 > 图文教程 > 网络编程 > PHP > How do I change MySQL timezone?

PHP
动易数据转成dedecms的php程序
PHP中文汉字验证码
收藏的一个php小偷的核心程序
ASP和PHP都是可以删除自身的
PHP+Tidy-完美的XHTML纠错+过滤
PHP实现MVC开发得最简单的方法——模型
相对路径转化成绝对路径
如何提高MYSQL数据库的查询统计速度 select 索引应用
php下的转义字符串
PHP 中英文混合排版中处理字符串常用的函数
Linux下ZendOptimizer的安装与配置方法
给apache2.2加上mod_encoding模块後 php5.2.0 处理url出现bug
安装PHP可能遇到的问题“无法载入mysql扩展” 的解决方法
dede全站URL静态化改造[070414更正]
使用Xdebug调试和优化PHP程序之[1]
PHP与SQL注入攻击[一]
PHP与SQL注入攻击[二]
PHP与SQL注入攻击[三]
PHP和XSS跨站攻击的防范
Discuz! 5.0.0论坛程序中加入一段js代码,让会员点击下载附件前自动弹出提示窗口

PHP 中的 How do I change MySQL timezone?


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 110 ::
收藏到网摘: n/a

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.