How do I change MySQL timezone?

yipeiwu_com5年前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.

相关文章

图解MYSQL的安装和数据升级第1/2页

图解MYSQL的安装和数据升级第1/2页

如果你是全新安装,就可以跳过这一步啦 找到你的Mysql的数据库目录下的 ragnarok 数据库目录,例如我的Mysql安装在D:\mysql 这个目录下,就到 D:\mysql\da...

mysql_num_rows VS COUNT 效率问题分析

mysql_num_rows 和 count( * ) 都能统计总数,那个能好一点呢? 或者 分别什么时候用num_rows 和 count( * )呢 一个直观的对比 测试数据: 条数...

PHP远程连接MYSQL数据库非常慢的解决方法

不知道如何解决,所以把他空间所在的服务器上也装了个MYSQL,才解决问题,今天又有个这个问题,不能也在这服务器上装一个MYSQL吧,Search: PHP远程连接MYSQL速度慢,有时远...

深入分析使用mysql_fetch_object()以对象的形式返回查询结果

mysql_fetch_object()同样用于获取查询数据结果集,返回当前行数据,并自动滑向下一行。但与mysql_fetch_row()和mysql_fetch_array()不同的...

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

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