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.

相关文章

PHP+MySQL实现模糊查询员工信息功能示例

PHP+MySQL实现模糊查询员工信息功能示例

本文实例讲述了PHP+MySQL实现模糊查询员工信息功能。分享给大家供大家参考,具体如下: 一、代码 注意两点: 1、用Notepad+编辑时,格式选择:【编码字符集】->【中文】...

apache+php+mysql安装配置方法小结

apache+php+mysql安装配置方法小结

整个安装流程如下: 1,首先安装apache:我安装的版本是: httpd-2.2.16-win32-x86-openssl-0.9.8o.msi 网址:http://www.apach...

WindowsXP中快速配置Apache+PHP5+Mysql

汗。废话不多说,先罗列出我下载的相关程序的版本: Mysql-4.1.8-essential-win Mysql-gui-tools-5.0-r12-win32 php-5.2.5-wi...

PHP将MySQL的查询结果转换为数组并用where拼接的示例

mysql查询结果转换为PHP数组的几种方法的区别:  $result = mysql_fetch_row():这个函数返回的是数组,数组是以数字作为下标的,你只能通过...

php中处理mysql_fetch_assoc返回来的数组 不用foreach----echo

复制代码 代码如下: $id = intval($_GET['id']); $row = $db->getResult($db->query("select * from "...