How do I change MySQL timezone?

yipeiwu_com6年前Mysql基础


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.

相关文章

mysql时区问题

用convert_tz转换时区,你可以用      show   variables  ...

从MySQL数据库表中取出随机数据的代码

MySQL 如何从表中取出随机数据  以前在群里讨论过这个问题,比较的有意思.mysql的语法真好玩. 他们原来都想用PHP的实现随机,但取出多条好像要进行两次...

PHP使用PDO、mysqli扩展实现与数据库交互操作详解

本文实例讲述了PHP使用PDO、mysqli扩展实现与数据库交互操作。分享给大家供大家参考,具体如下: 数据库 在我们开发php时,可能有人已经学习了php数据库的连接交互,也可能正准备...

发款php蜘蛛统计插件只要有mysql就可用

于是昨天便认真的做了一下,功能多一点,可以对各种搜索引擎统计分析。可以在多个时间段进行查看。其实代码很简单,为了更简洁些,代码压缩到6k.分为6个文件 1.安装程序 spilder_in...

php+mysqli实现将数据库中一张表信息打印到表格里的方法

本文实例讲述了php+mysqli实现将数据库中一张表信息(包括表头)打印到表格里的方法。分享给大家供大家参考。具体如下: 这段代码将就看吧。需要学习基础知识。代码如下: 复制代码 代码...