How do I change MySQL timezone?

yipeiwu_com4年前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  ...

PHP同时连接多个mysql数据库示例代码

实例: 复制代码 代码如下: <?php $conn1 = mysql_connect("127.0.0.1", "root","root","db1"); mysql_selec...

MYSQL 小技巧 -- LAST_INSERT_ID

其实,这两个是有区别的,LAST_INSERT_ID() 能返回 bigint 值的id。而,mysql_insert_id 返回的是 int 。如果你 的id 是 unsigned i...

MySQL数据库转移,access,sql server 转 MySQL 的图文教程

MySQL数据库转移,access,sql server 转 MySQL 的图文教程

ODBC是一种让各种数据库具有相同界面的应用程序界面先到php爱好者站的http://www.phpfans.net/download.php?cid=1&page=3下载mysql o...

php调用MySQL存储过程的方法集合(推荐)

类型一:调用带输入、输出类型参数的方法复制代码 代码如下:$returnValue = '';try { mysql_query ( "set @Return" ); ...