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的缓冲查询和无缓冲查询

关于缓冲查询和无缓冲查询 MySQL的客户端有两种类型的查询: 缓冲查询:将接收查询的结果并把他们存储在客户端的缓存中,而且接下来获取行记录的请求仅仅从本地内获取。 (1)优点:可以在结...

php将textarea数据提交到mysql出现很多空格的解决方法

本文实例讲述了php将textarea数据提交到mysql出现很多空格的解决方法。分享给大家供大家参考。具体分析如下: 有一些朋友可能会发现我们在html提交给php处理保存数据到mys...

PHP源码之 ext/mysql扩展部分

我写过一个外部模块扩展,现在开始看PHP源码中的mysql扩展,它是可以被集成到PHP内部的,所以应该算是内置的扩展了。 该扩展需要用到mysql数据库提供的一些接口,所以需要安装了my...

在VS2008中编译MYSQL5.1.48的方法

1、 下载MYSQL5.1.48源码,CMAKE,VS2008 2、 安装CMAKE和VS2008,解压MYSQL5.1.48到D:\mysql 3、 打开CMD;CD D:\mysql...

Php中用PDO查询Mysql来避免SQL注入风险的方法

当我们使用传统的 mysql_connect 、mysql_query方法来连接查询数据库时,如果过滤不严,就有SQL注入风险,导致网站被攻击,失去控制。虽然可以用mysql_real_...