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 相关文章推荐
PHPMyAdmin 快速配置方法
May 11 PHP
PHP得到某段时间区间的时间戳 php定时任务
Apr 12 PHP
PHP 使用memcached简单示例分享
Mar 05 PHP
PHP基于单例模式实现的mysql类
Jan 09 PHP
php实现将HTML页面转换成word并且保存的方法
Oct 14 PHP
php往mysql中批量插入数据实例教程
Dec 12 PHP
PHP中str_split()函数的用法讲解
Apr 11 PHP
PHP回调函数简单用法示例
May 08 PHP
thinkphp5.1框架中容器(Container)和门面(Facade)的实现方法分析
Aug 05 PHP
TP5框架请求响应参数实例分析
Oct 17 PHP
使用git迁移Laravel项目至新开发环境的步骤详解
Apr 06 PHP
PHP实现递归的三种方法
Jul 04 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图像处理类代码分享
2012/01/19 PHP
php的一个简单加密解密代码
2014/01/14 PHP
详解php curl带有csrf-token验证模拟提交方法
2018/04/18 PHP
PHP自定义函数实现assign()数组分配到模板及extract()变量分配到模板功能示例
2018/05/23 PHP
Thinkphp5框架异常处理操作实例分析
2020/06/03 PHP
一些常用的JS功能函数(2009-06-04更新)
2009/06/04 Javascript
JavaScript中神奇的call()方法
2015/03/12 Javascript
jQuery中trigger()与bind()用法分析
2015/12/18 Javascript
Vue2实现组件props双向绑定
2016/12/02 Javascript
jQuery点击弹出层弹出模态框点击模态框消失代码分享
2017/01/21 Javascript
浅谈angularjs依赖服务注入写法的注意点
2017/04/24 Javascript
基于jQuery封装的分页组件
2017/06/26 jQuery
Angular4编程之表单响应功能示例
2017/12/13 Javascript
vue项目中的webpack-dev-sever配置方法
2017/12/14 Javascript
JavaScript循环遍历你会用哪些之小结篇
2018/09/28 Javascript
JavaScript实现秒杀时钟倒计时
2019/09/29 Javascript
vue路由跳转传递参数的方式总结
2020/05/10 Javascript
[06:21]2014DOTA2国际邀请赛 庆祝VG首阶段领跑;B叔为挣牛排半夜整理情报
2014/07/13 DOTA
[38:21]2018DOTA2亚洲邀请赛3月30日 小组赛A组 LGD VS Newbee
2018/03/31 DOTA
python 随机数生成的代码的详细分析
2011/05/15 Python
python threading模块操作多线程介绍
2015/04/08 Python
在Django的form中使用CSS进行设计的方法
2015/07/18 Python
Python实现自动为照片添加日期并分类的方法
2017/09/30 Python
python 每天如何定时启动爬虫任务(实现方法分享)
2018/05/21 Python
多个应用共存的Django配置方法
2018/05/30 Python
python 含子图的gif生成时内存溢出的方法
2019/07/07 Python
详解如何减少python内存的消耗
2019/08/09 Python
应届生法律顾问求职信
2013/11/19 职场文书
考博专家推荐信模板
2013/12/02 职场文书
停车位租赁协议书
2014/09/24 职场文书
2014个人年终工作总结范文
2014/12/15 职场文书
努力学习保证书
2015/02/26 职场文书
.Net Core导入千万级数据至Mysql的步骤
2021/05/24 MySQL
python 中的jieba分词库
2021/11/23 Python
docker-compose部署Yapi的方法
2022/04/08 Servers
python中 Flask Web 表单的使用方法
2022/05/20 Python