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 相关文章推荐
Ajax+PHP 边学边练 之二 实例
Nov 24 PHP
PHP 读取文件内容代码(txt,js等)
Dec 06 PHP
《PHP编程最快明白》第六讲:Mysql数据库操作
Nov 01 PHP
php自动加载机制的深入分析
Jun 08 PHP
PHP安全的URL字符串base64编码和解码
Jun 19 PHP
PHP延迟静态绑定示例分享
Jun 22 PHP
thinkphp浏览历史功能实现方法
Oct 29 PHP
php实现mysql事务处理的方法
Dec 25 PHP
PHP人民币金额转大写实例代码
Oct 02 PHP
weiphp微信公众平台授权设置
Jan 04 PHP
PHP正则表达式入门教程(推荐)
May 18 PHP
php读取出一个文件夹及其子文件夹下所有文件的方法示例
Jun 15 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中常见的mongodb查询操作
2013/06/20 PHP
一漂亮的PHP图片验证码实例
2014/03/21 PHP
PHP中使用socket方式GET、POST数据实例
2015/04/02 PHP
PHP数组去重比较快的实现方式
2016/01/19 PHP
js中top/parent/frame概述及案例应用
2013/02/06 Javascript
jquery 滚动条事件简单实例
2013/07/12 Javascript
JS简单实现元素复制示例附图
2013/11/19 Javascript
Javascript前端UI框架Kit使用指南之kitjs事件管理
2014/11/28 Javascript
第一次动手实现bootstrap table分页效果
2016/09/22 Javascript
vue scroller返回页面记住滚动位置的实例代码
2018/01/29 Javascript
微信小程序文章详情页面实现代码
2018/09/10 Javascript
JS如何获取地址栏的参数实例讲解
2018/10/06 Javascript
微信小程序textarea层级过高(盖住其他元素)问题的解决办法
2019/03/04 Javascript
nodejs简单抓包工具使用详解
2019/08/23 NodeJs
创建与框架无关的JavaScript插件
2020/12/01 Javascript
[00:14]PWL:老朋友Mushi拍VLOG与中国玩家问好
2020/11/04 DOTA
详解Django框架中的视图级缓存
2015/07/23 Python
Python实现删除当前目录下除当前脚本以外的文件和文件夹实例
2015/07/27 Python
分享Python开发中要注意的十个小贴士
2016/08/30 Python
详解Python判定IP地址合法性的三种方法
2018/03/06 Python
python实现人人自动回复、抢沙发功能
2018/06/08 Python
python ftp 按目录结构上传下载的实现代码
2018/09/12 Python
Python3 Post登录并且保存cookie登录其他页面的方法
2018/12/28 Python
pytorch查看torch.Tensor和model是否在CUDA上的实例
2020/01/03 Python
python GUI库图形界面开发之PyQt5浏览器控件QWebEngineView详细使用方法
2020/02/26 Python
python实现取余操作的简单实例
2020/08/16 Python
python 实现图片修复(可用于去水印)
2020/11/19 Python
Django 实现图片上传和下载功能
2020/12/31 Python
贝尔帐篷精品店:Bell Tent Boutique
2019/06/12 全球购物
英语教育专业自荐信
2014/05/29 职场文书
关于读书的演讲稿1000字
2014/08/27 职场文书
赵乐秦在党的群众路线教育实践活动总结大会上的讲话稿
2014/10/25 职场文书
小学四年级班主任工作经验交流材料
2015/11/02 职场文书
《烈火英雄》观后感:致敬和平时代的英雄
2019/11/11 职场文书
PHP使用QR Code生成二维码实例
2021/07/07 PHP
关于springboot 配置date字段返回时间戳的问题
2021/07/25 Java/Android