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应用中使用XOR运算加密算法分享

XOR算法原理 从加密的主要方法看,换位法过于简单,特别是对于数据量少的情况很容易由密文猜出明文,而替换法不失为一种行之有效的简易算法。 从各种替换法运算的特点看,异或运算最适合用于简易...

php下mysql数据库操作类(改自discuz)

复制代码 代码如下: <?php /* -------------------------------- System:PT book - PT小说小偷 Code: 杰少Pakey...

mysql desc(DESCRIBE)命令实例讲解

mysql desc命令用于查看表结构,是DESCRIBE命令的简写形式。 mysql desc命令语法: desc tablename 该命令会显示表哪些信息呢?具体包括: 字...

mysql5写入和读出乱码解决

我写的例子 复制代码 代码如下:<?  require("adodb/adodb.inc.php");  $conn=newadoconnection('mys...

WindowsXP中快速配置Apache+PHP5+Mysql

汗。废话不多说,先罗列出我下载的相关程序的版本: Mysql-4.1.8-essential-win Mysql-gui-tools-5.0-r12-win32 php-5.2.5-wi...