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用户指南-cookies部分
Oct 09 PHP
php htmlentities和htmlspecialchars 的区别
Aug 18 PHP
php5.5新数组函数array_column使用
Jul 08 PHP
php实现统计邮件大小的方法
Aug 06 PHP
php中sql注入漏洞示例 sql注入漏洞修复
Jan 24 PHP
destoon供应信息title调用出公司名称的方法
Aug 22 PHP
百万级别知乎用户数据抓取与分析之PHP开发
Sep 28 PHP
PHP5.5迭代生成器用法实例详解
Mar 16 PHP
php_pdo 预处理语句详解
Nov 21 PHP
php实现连接access数据库并转txt写入的方法
Feb 08 PHP
PHP实现APP微信支付的实例讲解
Feb 10 PHP
ThinkPHP3.2.3框架邮件发送功能图文实例详解
Apr 23 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中的正规表达式(二)
2006/10/09 PHP
php Notice: Undefined index 错误提示解决方法
2010/08/29 PHP
PHP命名空间namespace用法实例分析
2016/09/27 PHP
Laravel基础_关于view共享数据的示例讲解
2019/10/14 PHP
利用百度地图JSAPI生成h7n9禽流感分布图实现代码
2013/04/15 Javascript
jquery用data方法获取某个元素上的事件
2014/06/23 Javascript
jQuery qrcode生成二维码的方法
2016/04/03 Javascript
详解ElementUI之表单验证、数据绑定、路由跳转
2017/06/21 Javascript
jquery图片放大镜效果
2017/06/23 jQuery
js+html5实现页面可刷新的倒计时效果
2017/07/15 Javascript
jQuery实现的模仿雨滴下落动画效果
2018/12/11 jQuery
微信小程序点击顶部导航栏切换样式代码实例
2019/11/12 Javascript
react-router-dom 嵌套路由的实现
2020/05/02 Javascript
Vue切换div显示隐藏,多选,单选代码解析
2020/07/14 Javascript
前端vue+elementUI如何实现记住密码功能
2020/09/20 Javascript
Python multiprocessing模块中的Pipe管道使用实例
2015/04/11 Python
python Django批量导入数据
2016/03/25 Python
Python 爬虫学习笔记之多线程爬虫
2016/09/21 Python
Django中使用celery完成异步任务的示例代码
2018/01/23 Python
python3安装pip3(install pip3 for python 3.x)
2018/04/03 Python
深入浅析Python获取对象信息的函数type()、isinstance()、dir()
2018/09/17 Python
Python object类中的特殊方法代码讲解
2020/03/06 Python
快速解决jupyter启动卡死的问题
2020/04/10 Python
什么是python的id函数
2020/06/11 Python
python等待10秒执行下一命令的方法
2020/07/19 Python
python爬虫中url管理器去重操作实例
2020/11/30 Python
某/etc/fstab文件中的某行如下: /dev/had5 /mnt/dosdata msdos defaults,usrquota 1 2 请解释其含义
2013/04/11 面试题
MYSQL基础面试题
2012/05/13 面试题
优秀的茶餐厅创业计划书
2014/01/03 职场文书
护士辞职信模板
2014/01/20 职场文书
父母对孩子的寄语
2014/04/09 职场文书
《长征》教学反思
2014/04/27 职场文书
大学生先进个人主要事迹材料
2015/11/04 职场文书
拔河比赛队名及霸气口号
2015/12/24 职场文书
Laravel中获取IP的真实地理位置
2021/04/01 PHP
JavaScript的function函数详细介绍
2021/11/20 Javascript