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 rand()造成CPU 100%的解决办法

百度查阅了一些资料,再结合自己的一些经验,采用以下解决办法: 复制代码 代码如下: $idlist=''; for($i=1;$i<=20;$i++){ if($i==1){ $i...

对比PHP对MySQL的缓冲查询和无缓冲查询

关于缓冲查询和无缓冲查询 MySQL的客户端有两种类型的查询: 缓冲查询:将接收查询的结果并把他们存储在客户端的缓存中,而且接下来获取行记录的请求仅仅从本地内获取。 (1)优点:可以在结...

PHP+MySQL实现模糊查询员工信息功能示例

PHP+MySQL实现模糊查询员工信息功能示例

本文实例讲述了PHP+MySQL实现模糊查询员工信息功能。分享给大家供大家参考,具体如下: 一、代码 注意两点: 1、用Notepad+编辑时,格式选择:【编码字符集】->【中文】...

解决PHP在DOS命令行下却无法链接MySQL的技术笔记

正好今天朋友 xjb 也碰到了这个问题,所以写了这篇笔记,将此问题的描述以及解决记录下。 问题描述:用 web 方式, 可以链接 mysql, 但是在命令行下, 却提示:   Fatal...

教你如何快捷的使用cmd访问mysql小技巧

教你如何快捷的使用cmd访问mysql小技巧

以window7为例,右击“计算机” - 单击“属性” - 单击“高级系统设置” - 单击“环境变量”,剩下看图: <图1> 右下角"环境变量". <图2>选择...