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事务实例解析

本文实例讲解了PHP使用MySQL事物的实例,并备有注释加以详细说明。分享给大家供大家参考之用。 具体实例如下所示: <?php //数据库连接 $conn = mys...

PHP访问MYSQL数据库封装类(附函数说明)

复制代码 代码如下:<?php /* MYSQL 数据库访问封装类 MYSQL 数据访问方式,php4支持以mysql_开头的过程访问方式,php5开始支持以mysqli_开头的过...

PHP连接MySQL数据的操作要点

MySQL扩展库操作MySQL数据库的步骤如下: 1:获取连接. 2:选取书库。 3:设置操作编码。 4:发送SQL指令(MySQL数据库可以分为四种指令: 4.1:ddl: 数据定义语...

mysql_escape_string()函数用法分析

本文实例讲述了mysql_escape_string()函数用法。分享给大家供大家参考,具体如下: 使用 mysql_escape_string() 对查询中有疑问的数据进行编码: 有...

PHP+mysql实现从数据库获取下拉树功能示例

本文实例讲述了PHP+mysql实现从数据库获取下拉树功能。分享给大家供大家参考,具体如下: <?php include "config.php"; include "...