How do I change MySQL timezone?

yipeiwu_com5年前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基于Fleaphp框架实现cvs数据导入MySQL的方法

本文实例讲述了php基于Fleaphp框架实现cvs数据导入MySQL的方法。分享给大家供大家参考,具体如下: <?php /* * To change this t...

php简单备份与还原MySql的方法

本文实例讲述了php简单备份与还原MySql的方法。分享给大家供大家参考,具体如下: 一、备份: <?php header ( "content-Type: text/...

解析mysql 表中的碎片产生原因以及清理

大量删除数据必然会在数据文件中造成不连续的空白空间,而当插入数据时,这些空白空间则会被利用起来 。对于不同的存储引擎整理碎片的方式不一样。myisam可以有以下方式:mysql>...

mysql_fetch_row,mysql_fetch_array,mysql_fetch_assoc的区别

复制代码 代码如下:<?php $link=mysql_connect('localhost','root',”); mysql_select_db('abc',$link); $...

PHP连接MySQL数据库并以json格式输出

1.简介 PHP连接数据库有多种方法,现介绍常用的MySQL数据库连接方法,PHP连接MySQL也有两种方式,一是面向对象,二是面向过程方式,两种方法稍有区别。下面通过代码介绍两种方法连...