How do I change MySQL timezone?


Posted in PHP onMarch 26, 2008

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 相关文章推荐
十天学会php之第七天
Oct 09 PHP
PHP扩展编写点滴 技巧收集
Mar 09 PHP
PHP中将字符串转化为整数(int) intval() printf() 性能测试
Mar 20 PHP
phpExcel导出大量数据出现内存溢出错误的解决方法
Feb 28 PHP
利用curl抓取远程页面内容的示例代码
Jul 23 PHP
php实现cc攻击防御和防止快速刷新页面示例
Feb 13 PHP
基于CakePHP实现的简单博客系统实例
Jun 28 PHP
WordPress中使主题支持小工具以及添加插件启用函数
Dec 22 PHP
前端必学之PHP语法基础
Jan 01 PHP
PHP微信模板消息操作示例
Jun 29 PHP
php弹出提示框的是实例写法
Sep 26 PHP
Aliyun Linux 编译安装 php7.3 tengine2.3.2 mysql8.0 redis5的过程详解
Oct 20 PHP
有关 PHP 和 MySQL 时区的一点总结
Mar 26 #PHP
使用 MySQL Date/Time 类型
Mar 26 #PHP
MySQL修改密码方法总结
Mar 25 #PHP
用phpmyadmin更改mysql5.0登录密码
Mar 25 #PHP
常用的php ADODB使用方法集锦
Mar 25 #PHP
PHP中ADODB类详解
Mar 25 #PHP
php下判断数组中是否存在相同的值array_unique
Mar 25 #PHP
You might like
php中的mongodb select常用操作代码示例
2014/09/06 PHP
smarty自定义函数htmlcheckboxes用法实例
2015/01/22 PHP
js switch case default 的用法示例介绍
2013/10/23 Javascript
js使用for循环查询数组中是否存在某个值
2014/08/12 Javascript
jquery delay()介绍及使用指南
2014/09/02 Javascript
JavaScript中的值是按值传递还是按引用传递问题探讨
2015/01/30 Javascript
js实现div在页面拖动效果
2016/05/04 Javascript
javascript函数的四种调用模式
2017/01/08 Javascript
ionic中列表项增加和删除的实现方法
2017/01/22 Javascript
web3.js增加eth.getRawTransactionByHash(txhash)方法步骤
2018/03/15 Javascript
vue router+vuex实现首页登录验证判断逻辑
2018/05/17 Javascript
最后说说Vue2 SSR 的 Cookies 问题
2018/05/25 Javascript
video.js 实现视频只能后退不能快进的思路详解
2018/08/09 Javascript
Vue props中Object和Array设置默认值操作
2020/07/30 Javascript
Python实现读写INI配置文件的方法示例
2018/06/09 Python
python梯度下降法的简单示例
2018/08/31 Python
Python下简易的单例模式详解
2019/04/08 Python
PyQtGraph在pyqt中的应用及安装过程
2019/08/04 Python
python Dijkstra算法实现最短路径问题的方法
2019/09/19 Python
浅谈python处理json和redis hash的坑
2020/07/16 Python
详解python的xlwings库读写excel操作总结
2021/02/26 Python
King Apparel官网:英国街头服饰品牌
2019/09/05 全球购物
linux面试题参考答案(8)
2016/04/19 面试题
班组长安全生产职责
2013/12/16 职场文书
幼儿园实习生辞职信
2014/01/20 职场文书
校园十大歌手策划书
2014/02/01 职场文书
对祖国的寄语大全
2014/04/11 职场文书
安全资料员岗位职责范本
2014/06/28 职场文书
合伙购房协议样本
2014/10/06 职场文书
工作失职检讨书500字
2014/10/17 职场文书
滞留工资返还协议书
2014/10/19 职场文书
大学生毕业评语
2014/12/31 职场文书
酒店财务部岗位职责
2015/04/14 职场文书
专项资金申请报告
2015/05/15 职场文书
生日寿星公答谢词
2015/09/29 职场文书
Django框架中模型的用法
2022/06/10 Python