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+mysql 采用ajax技术的 省 市 地 3级联动无刷新菜单 源码
Dec 16 PHP
php5数字型字符串加解密代码
Apr 24 PHP
drupal 代码实现URL重写
May 04 PHP
PHP使用strtotime计算两个给定日期之间天数的方法
Mar 18 PHP
4种PHP异步执行的常用方式
Dec 24 PHP
PHP 序列化和反序列化函数实例详解
Jul 18 PHP
php readfile下载大文件失败的解决方法
May 22 PHP
Yii2中简单的场景使用介绍
Jun 02 PHP
PHP使用new StdClass()创建空对象的方法分析
Jun 06 PHP
PHP 7安装使用体验之性能大提升,兼容性强,扩展支持不够(升级PHP要谨慎)
Jul 27 PHP
实例说明js脚本语言和php脚本语言的区别
Apr 04 PHP
laravel config文件配置全局变量的例子
Oct 13 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
linux php mysql数据库备份实现代码
2009/03/10 PHP
PHP输出日历表代码实例
2015/03/27 PHP
javascript基础知识大集锦(二) 推荐收藏
2011/01/13 Javascript
jquery中ajax函数执行顺序问题之如何设置同步
2014/02/28 Javascript
JavaScript中的索引数组、关联数组和静态数组、动态数组讲解
2014/11/08 Javascript
JavaScript中匿名函数用法实例
2015/03/23 Javascript
jquery+json实现数据二级联动的方法
2015/11/28 Javascript
瀑布流的实现方式(原生js+jquery+css3)
2020/06/28 Javascript
详解.vue文件中监听input输入事件(oninput)
2017/09/19 Javascript
Vue2仿淘宝实现省市区三级联动
2020/04/15 Javascript
小程序实现五星点评效果
2018/11/03 Javascript
vue 使用外部JS与调用原生API操作示例
2019/12/02 Javascript
[02:42]DOTA2英雄基础教程 杰奇洛
2013/12/23 DOTA
python批量提交沙箱问题实例
2014/10/08 Python
Python装饰器decorator用法实例
2014/11/10 Python
itchat和matplotlib的结合使用爬取微信信息的实例
2017/08/25 Python
Python实现的将文件每一列写入列表功能示例【测试可用】
2018/03/19 Python
pytorch对可变长度序列的处理方法详解
2018/12/08 Python
详解如何在Apache中运行Python WSGI应用
2019/01/02 Python
Python分析彩票记录并预测中奖号码过程详解
2019/07/09 Python
python基于gevent实现并发下载器代码实例
2019/11/01 Python
Python socket模块方法实现详解
2019/11/05 Python
基于python求两个列表的并集.交集.差集
2020/02/10 Python
完美解决pyinstaller打包报错找不到依赖pypiwin32或pywin32-ctypes的错误
2020/04/01 Python
使用OpenCV对车道进行实时检测的实现示例代码
2020/06/19 Python
canvas 实现 github404动态效果的示例代码
2017/11/15 HTML / CSS
美国领先的户外服装与装备用品店:Moosejaw
2016/08/25 全球购物
80年代复古T恤:TruffleShuffle
2018/07/02 全球购物
严选全球尖货,立足香港:Bonpont宝盆
2018/07/24 全球购物
马来西亚和新加坡巴士票在线预订:CatchThatBus
2018/11/17 全球购物
Linux内核产生并发的原因
2012/07/13 面试题
一名女生的自荐信
2013/12/08 职场文书
大学生万能检讨书范例
2014/10/04 职场文书
2014年质量工作总结
2014/11/22 职场文书
医学生自荐信范文(2016精选篇)
2016/01/28 职场文书
Spring Boot mybatis-config 和 log4j 输出sql 日志的方式
2021/07/26 Java/Android