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 相关文章推荐
Google Voice 短信发送接口PHP开源版(2010.5更新)
Jul 22 PHP
PHP字符串长度计算 - strlen()函数使用介绍
Oct 15 PHP
PHP学习笔记之字符串编码的转换和判断
May 22 PHP
PHP开发微信支付的代码分享
May 25 PHP
WordPress中注册菜单与调用菜单的方法详解
Dec 18 PHP
Zend Framework框架Smarty扩展实现方法
Mar 22 PHP
THinkPHP获取客户端IP与IP地址查询的方法
Nov 14 PHP
PHP 信号管理知识整理汇总
Feb 19 PHP
PHP使用PHPExcel实现批量上传到数据库的方法
Jun 08 PHP
全面解析PHP面向对象的三大特征
Jun 10 PHP
php记录搜索引擎爬行记录的实现代码
Mar 02 PHP
PHP实现断点续传乱序合并文件的方法
Sep 06 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 GD 图像处理组件的常用函数总结
2010/04/28 PHP
PHP序列号生成函数和字符串替换函数代码
2012/06/07 PHP
php笔记之:php函数range() round()和list()的使用说明
2013/04/26 PHP
百度工程师讲PHP函数的实现原理及性能分析(一)
2015/05/13 PHP
如何通过View::first使用Laravel Blade的动态模板详解
2017/09/21 PHP
Document 对象的常用方法
2009/07/31 Javascript
jquery实现弹出层遮罩效果的简单实例
2014/03/03 Javascript
jQuery调取jSon数据并展示的方法
2015/01/29 Javascript
jQuery插件zepto.js简单实现tab切换
2015/06/16 Javascript
js基础之DOM中元素对象的属性方法详解
2016/10/28 Javascript
ECMAScript6--解构
2017/03/30 Javascript
vue+echarts实现动态绘制图表及异步加载数据的方法
2018/10/17 Javascript
JavaScript数据结构与算法之基本排序算法定义与效率比较【冒泡、选择、插入排序】
2019/02/21 Javascript
python实现根据窗口标题调用窗口的方法
2015/03/13 Python
Python的Flask开发框架简单上手笔记
2015/11/16 Python
Python中装饰器兼容加括号和不加括号的写法详解
2017/07/05 Python
Python使用Turtle模块绘制五星红旗代码示例
2017/12/11 Python
详解10个可以快速用Python进行数据分析的小技巧
2019/06/24 Python
在python 中split()使用多符号分割的例子
2019/07/15 Python
python requests更换代理适用于IP频率限制的方法
2019/08/21 Python
python 并发下载器实现方法示例
2019/11/22 Python
Python urllib.request对象案例解析
2020/05/11 Python
Html5页面获取微信公众号的openid的方法
2020/05/12 HTML / CSS
朗仕(Lab series)英国官网:雅诗兰黛集团男士专属护肤品牌
2017/11/28 全球购物
蹦床仓库:Trampoline Warehouse
2018/12/06 全球购物
澳洲CFL商城:CHEMIST FOR LESS(中文)
2021/02/28 全球购物
销售人员中英文自荐信
2013/09/22 职场文书
职业教育毕业生求职信
2013/11/09 职场文书
机电专业个人求职信范文
2013/12/30 职场文书
中式面点餐厅创业计划书
2014/01/29 职场文书
预备党员2014全国两会学习心得体会
2014/03/10 职场文书
个人委托书范本
2014/09/13 职场文书
社区学习党的群众路线教育实践活动心得体会
2014/11/03 职场文书
2015年社区中秋节活动总结
2015/03/23 职场文书
mysql left join快速转inner join的过程
2021/06/30 MySQL
javascript的var与let,const之间的区别详解
2022/02/18 Javascript