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_pconnect()函数使用介绍

mysql_pconnect在单一进程,特别是一直保持执行的监控程序,特别适用。 mysql_pconnect用法和mysql_connect类似: 复制代码 代码如下: <?ph...

PHP+MYSQL会员系统的开发实例教程

本文通过一个简单的实例完成了完整的PHP+MySQL会员系统功能。是非常实用的一个应用。具体实现步骤如下: 一、会员系统的原理:     登陆-->判断--&g...

DW中链接mysql数据库时,建立字符集中文出现乱码的解决方法

只是中文出现乱码时,在链接数据库后面,加上这一句 utf8的话 mysql_query("SET NAMES 'utf8'"); gbk的话 mysql_query("SET NAMES...

整理的一些实用WordPress后台MySQL操作命令

不过假设你的WordPress网站上有成百上千篇文章,而你需要进行全站范围的改动, 这时从后台逐条编辑就有点费时费力了,并且犯错的几率也会提高。 最好的方法是进入WordPress的My...

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

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