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

PHP
PHP与MySQL开发中页面出现乱码的一种解决方法
在PHP里得到前天和昨天的日期的代码
PHP4和PHP5性能测试和对比 测试代码与环境
wordpress之wp-settings.php
PHP下几种删除目录的方法总结
discuz 首页四格:最新话题+最新回复+热门话题+精华文章插件
海河写的 Discuz论坛帖子调用js的php代码
利用static实现表格的颜色隔行显示的代码
从一个不错的留言本弄的mysql数据库操作类
从MySQL数据库表中取出随机数据的代码
56.com视频采集接口程序(PHP)
php下实现伪 url 的超简单方法[转]
一些常用的php简单命令代码集锦
用windows下编译过的eAccelerator for PHP 5.1.6实现php加速的使用方法
实现php加速的eAccelerator dll支持文件打包下载
使用 eAccelerator加速PHP代码的方法
pw的一个放后门的方法分析
php在线生成ico文件的代码
一个图形显示IP的PHP程序代码
[PHP]实用函数2

PHP 中的 How do I change MySQL timezone?


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 107 ::
收藏到网摘: 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.