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 相关文章推荐
杏林同学录(五)
Oct 09 PHP
PHP基础知识回顾
Aug 16 PHP
限制ckeditor上传图片文件大小的方法
Nov 15 PHP
ThinkPHP字符串函数及常用函数汇总
Jul 18 PHP
PHP return语句另类用法不止是在函数中
Sep 17 PHP
PHP判断数据库中的记录是否存在的方法
Nov 14 PHP
php使用CURL伪造IP和来源实例详解
Jan 15 PHP
关于WordPress的SEO优化相关的一些PHP页面脚本技巧
Dec 10 PHP
php实现的顺序线性表示例
May 04 PHP
php依赖注入知识点详解
Sep 23 PHP
PHP设计模式之装饰器(装饰者)模式(Decorator)入门与应用详解
Dec 13 PHP
PHP中mysqli_get_server_version()的实例用法
Feb 03 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
重置版游戏视频
2020/04/09 魔兽争霸
PHP去掉从word直接粘贴过来的没有用格式的函数
2012/10/29 PHP
php-fpm配置详解
2014/02/12 PHP
PHP开启opcache提升代码性能
2015/04/26 PHP
php目录拷贝实现方法
2015/07/10 PHP
通过源码解析Laravel的依赖注入
2018/01/22 PHP
JavaScript中的Array对象使用说明
2011/01/17 Javascript
让低版本浏览器支持input的placeholder属性(js方法)
2013/04/03 Javascript
简单常用的幻灯片播放实现代码
2013/09/25 Javascript
动态读取JSON解析键值对的方法
2014/06/03 Javascript
用简洁的jQuery方法toggleClass实现隔行换色
2014/10/22 Javascript
深入理解javascript严格模式(Strict Mode)
2014/11/28 Javascript
JavaScript Math.floor方法(对数值向下取整)
2015/01/09 Javascript
jquery带有索引按钮且自动轮播切换特效代码分享
2015/09/15 Javascript
基于JavaScript实现移动端点击图片查看大图点击大图隐藏
2015/11/04 Javascript
JavaScript动态创建div等元素实例讲解
2016/01/06 Javascript
微信小程序 视图容器组件的详解及实例代码
2017/01/19 Javascript
js 数据存储和DOM编程
2017/02/09 Javascript
使用vue for时为什么要key【推荐】
2019/07/11 Javascript
微信小程序自定义navigationBar顶部导航栏适配所有机型(附完整案例)
2020/04/26 Javascript
微信小程序 获取手机号 JavaScript解密示例代码详解
2020/05/14 Javascript
Element-ui树形控件el-tree自定义增删改和局部刷新及懒加载操作
2020/08/31 Javascript
jQuery是用来干什么的 jquery其实就是一个js框架
2021/02/04 jQuery
Python实现控制台进度条功能
2016/01/04 Python
分享6个隐藏的python功能
2017/12/07 Python
对python GUI实现完美进度条的示例详解
2018/12/13 Python
python scrapy重复执行实现代码详解
2019/12/28 Python
Charles & Colvard官网:美国莫桑石品牌
2019/06/05 全球购物
Vinatis德国:法国领先的葡萄酒邮购公司
2020/09/07 全球购物
翻译学院毕业生自荐书
2014/02/02 职场文书
餐厅总厨求职信
2014/03/04 职场文书
八荣八耻的活动方案
2014/08/16 职场文书
2015年征兵工作总结
2015/07/23 职场文书
安全伴我行主题班会
2015/08/13 职场文书
《进击的巨人》新联动CM 兵长强势出击兽巨人
2022/04/05 日漫
springboot应用服务启动事件的监听实现
2022/04/06 Java/Android