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 相关文章推荐
15种PHP Encoder的比较
Apr 17 PHP
PHP求最大子序列和的算法实现
Jun 24 PHP
使用PHP计算两个路径的相对路径
Jun 14 PHP
md5 16位二进制与32位字符串相互转换示例
Dec 30 PHP
php fsockopen解决办法 php实现多线程
Jan 20 PHP
关于WordPress的SEO优化相关的一些PHP页面脚本技巧
Dec 10 PHP
Thinkphp3.2.3分页使用实例解析
Jul 28 PHP
PHP抓取远程图片(含不带后缀的)教程详解
Oct 21 PHP
PHP面向对象之领域模型+数据映射器实例(分析)
Jun 21 PHP
PHP实现表单提交数据的验证处理功能【防SQL注入和XSS攻击等】
Jul 21 PHP
TP3.2批量上传文件或图片 同名冲突问题的解决方法
Aug 01 PHP
总结PHP中初始化空数组的最佳方法
Feb 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
ThinkPHP结合AjaxFileUploader实现无刷新文件上传的方法
2014/10/29 PHP
PHP获取当前相对于域名目录的方法
2015/06/26 PHP
PHP搭建大文件切割分块上传功能示例
2017/01/04 PHP
PHP严重致命错误处理:php Fatal error: Cannot redeclare class or function
2017/02/05 PHP
javascript生成/解析dom的CDATA类型的字段的代码
2007/04/22 Javascript
再论Javascript的类继承
2011/03/05 Javascript
js变量以及其作用域详解
2020/07/18 Javascript
window.onload和$(function(){})的区别介绍
2013/10/30 Javascript
jQuery $.each遍历对象、数组用法实例
2015/04/16 Javascript
javascript 中的 delete及delete运算符
2015/11/15 Javascript
Bootstrap精简教程
2015/11/27 Javascript
jquery实现下拉框功能效果【实例代码】
2016/05/06 Javascript
JS常用字符串方法(推荐)
2021/01/15 Javascript
JS实现的自动打字效果示例
2017/03/10 Javascript
Vue2.0实现1.0的搜索过滤器功能实例代码
2017/03/20 Javascript
详解用Node.js写一个简单的命令行工具
2018/03/01 Javascript
Vue 框架之动态绑定 css 样式实例分析
2018/11/14 Javascript
jQuery实现弹出层效果
2019/12/10 jQuery
Vue 组件的挂载与父子组件的传值实例
2020/09/02 Javascript
python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
2014/08/22 Python
python获取指定目录下所有文件名列表的方法
2015/05/20 Python
Python多线程经典问题之乘客做公交车算法实例
2017/03/22 Python
Python基于回溯法子集树模板解决马踏棋盘问题示例
2017/09/11 Python
Python面向对象程序设计之类的定义与继承简单示例
2019/03/18 Python
python批量下载抖音视频
2019/06/17 Python
html5 touch事件实现页面上下滑动效果【附代码】
2016/03/10 HTML / CSS
英国首屈一指的票务公司:See Tickets
2019/05/11 全球购物
戴尔马来西亚官网:Dell Malaysia
2020/05/02 全球购物
应聘美工求职信
2013/11/07 职场文书
培训班主持词
2014/03/28 职场文书
竞聘自述材料
2014/08/25 职场文书
一年级语文下册复习计划
2015/01/17 职场文书
返乡农民工证明
2015/06/24 职场文书
小学生节约用水倡议书
2019/08/12 职场文书
redis内存空间效率问题的深入探究
2021/05/17 Redis
vue el-table实现递归嵌套的示例代码
2022/08/14 Vue.js