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.

相关文章

解析link_mysql的php版

复制代码 代码如下:<?php$str_sql_read="select count(*) as num from userinfo";$str_sql_del="delete f...

PHP与MySQL开发中页面出现乱码的一种解决方法

一般来说,乱码的出现有2种原因,首先是由于编码(charset)设置错误,导致浏览器以错误的编码来解析,从而出现了满屏乱七八糟的“天书”,其次是文件被以错误的编码打开,然后保存,比如一个...

flash+php+mysql打造简单留言本教程第1/3页

flash+php+mysql打造简单留言本教程第1/3页

(主要参考了火山的帖子:★FLASH与ASP通信入门教程——做真正属于自己的留言本!)。网上没有比较好的php留言本相关教程,我下载的N多源文件都看得云里雾里,而且好多都将代码写在MC上...

PHP封装mysqli基于面向对象的mysql数据库操作类与用法示例

本文实例讲述了PHP封装mysqli基于面向对象的mysql数据库操作与用法。分享给大家供大家参考,具体如下: 首先封装好mysql类 mysql.php <?php...

PHP基于ORM方式操作MySQL数据库实例

PHP基于ORM方式操作MySQL数据库实例

本文实例讲述了PHP基于ORM方式操作MySQL数据库。分享给大家供大家参考,具体如下: ORM----Oriented Relationship Mapper,即用面向对象的方式来操作...