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读取ACCESS数据到MYSQL的代码

复制代码 代码如下: <?php header('ontent-Type:text/html;charset=GB2312');//避免输出乱码 $dbhost ="localho...

从MySQL数据库表中取出随机数据的代码

MySQL 如何从表中取出随机数据  以前在群里讨论过这个问题,比较的有意思.mysql的语法真好玩. 他们原来都想用PHP的实现随机,但取出多条好像要进行两次...

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

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

一款简单实用的php操作mysql数据库类

本文实例讲述了一款简单实用的php操作mysql数据库类。分享给大家供大家参考。具体如下: 复制代码 代码如下: /* 本款数据库连接类,他会自动加载sql防注入功能,过滤一些敏感的s...

MYSQL环境变量设置方法

mysql环境变量设置(windows环境) 1、对于命令控来说,在cmd命令下行下操作很方便,但有时需要直接在命令行床空执行mysql这样的命令,会出现”mysql不是内部或外部命令“...