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 SQL Injection with MySQL

php SQL Injection with MySQL

前言   2003年开始,喜欢脚本攻击的人越来越多,而且研究ASP下注入的朋友也逐渐多了起来,我看过最早的关于SQL注入的文章是一篇99年国外的高手写的,而现在国外的已经炉火纯青了,国内...

在php和MySql中计算时间差的方法

最近在研究自己爱围脖的时候就要计算到恋爱天数,这需要php根据每天的日期进行计算,下面就来谈谈实现这种日期计算的几种方法: (1) 如果有数据库就很容易了!若是MSSQL可以使用触发器!...

浅析Mysql 数据回滚错误的解决方法

MYSQL的事务处理主要有两种方法。1、用begin,rollback,commit来实现begin 开始一个事务rollback 事务回滚commit 事务确认 2、直接用set来改变...

PHP实现清除MySQL死连接的方法

本文实例讲述了PHP实现清除MySQL死连接的方法。分享给大家供大家参考,具体如下: 连接的情况,主要表现为有过多的Sleep连接,并且Time时间很长,占满了所有的可用连接数,以至于其...

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

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

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