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运行环境(Apache配置、Mysql)搭建安装图文教程

非集成环境的php运行环境(Apache配置、Mysql)搭建安装图文教程

本文为大家分享了php运行环境搭建安装图文教程,供大家参考,具体内容如下 安装apache: 1,不要安装到有中文的目录中: 2,尽量将apache,php,mysql安装到一个总的目录...

PHP实现PDO操作mysql存储过程示例

本文实例讲述了PHP实现PDO操作mysql存储过程。分享给大家供大家参考,具体如下: 一 代码 sql语句: create procedure pro_reg (in nc var...

PHP6 mysql连接方式说明

PHP6 mysql连接方式说明

mysqlnd是在PHP源码树中集成,与原先的libmysql不同,mysqlnd与内核联系更紧密. 官方说内存占用要节省40%左右.速度也更快. 顺便提一下.如果在升级到PHP5.3以...

mac下Apache + MySql + PHP搭建网站开发环境

首先为什不自己分别搭建Apache,PHP和MySql的环境呢?这样自己可以了解更多知识,说起来也更酷。可也许因为我懒吧,我是那种“既然有现成的,用就是了”的人。君子生非异也,善假于物也...

php+MySQL实现登录时验证登录名和密码是否正确

php+MySQL实现登录时验证登录名和密码是否正确

直入主题,先看php校验登录名和密码是否正确的代码: <?php $servername = "服务器名"; $username = "账户名"; $passw...