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

PHP
用PHP生成html分页列表的代码
php中的session完全教程
PHP session常见问题集锦及解决办法总结
PHP 中dirname(_file_)讲解
PHP_MySQL教程-第一天
PHP_MySQL教程-第二天while循环与数据库操作
PHP_MySQL教程-第三天 基本函数
一篇不错的PHP基础学习笔记
PHP5中的this,self和parent关键字详解教程
phpMyAdmin 安装教程全攻略
PHP入门速成教程
php之字符串变相相减的代码
php中的实现trim函数代码
IIS6的PHP最佳配置方法
简单介绍下 PHP5 中引入的 MYSQLI的用途
php分页示例代码
生成静态页面的php函数,php爱好者站推荐
隐藏你的.php文件的实现方法
推荐一篇入门级的Class文章
PHP配置文件中最常用四个ini函数

PHP 中的 How do I change MySQL timezone?


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