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.

相关文章

1亿条数据如何分表100张到Mysql数据库中(PHP)

下面通过创建100张表来演示下1亿条数据的分表过程,具体请看下文代码。 当数据量猛增的时候,大家都会选择库表散列等等方式去优化数据读写速度。笔者做了一个简单的尝试,1亿条数据,分100张...

php 将excel导入mysql

这里介绍一个直接将excel文件导入mysql的例子。我花了一晚上的时间测试,无论导入简繁体都不会出现乱码,非常好用。PHP-ExcelReader,下载地址: http://sourc...

使用Limit参数优化MySQL查询的方法

前些天看了一个老外写的程序,在 MySQL 查询中使用了很多 Limit 关键字,这就让我很感兴趣了,因为在我印象中, Limit 关键字似乎更多被使用 MySQL 数据库的程序员用来做...

使用PHP连接多种数据库的实现代码(mysql,access,sqlserver,Oracle)

1、PHP连接MYSQL数据库的代码 <?php $mysql_server_name='localhost'; //改成自己的mysql数据库服务器 $mys...

PHP+MySQL使用mysql_num_rows实现模糊查询图书信息功能

PHP+MySQL使用mysql_num_rows实现模糊查询图书信息功能

本文实例讲述了PHP+MySQL使用mysql_num_rows实现模糊查询图书信息功能。分享给大家供大家参考,具体如下: 一、代码 td{ font-size:9pt; } .s...