MySQL時區處理

開發人員執行以下SQL
html

root@localhost{wm_ztcj} >select  timestampdiff(second,'1970-1-1','2014-07-23 9:18:40') as timestamp;
+------------+
| timestamp  |
+------------+
| 1406107120 |
+------------+
1 row in set (0.00 sec)

在代碼中傳遞獲取到的時間戳並用from_unixtime函數還原日期mysql

mysql> select from_unixtime(1406107120);
+---------------------------+
| from_unixtime(1406107120) |
+---------------------------+
| 2014-07-23 17:18:40       |
+---------------------------+
1 row in set (0.00 sec)

返回的時間值相差8個小時,查看當前系統的時間戳
sql

root@localhost{wm_ztcj} >\! date
Wed Jul 23 10:49:09 CST 2014

查看數據庫的時區設定shell

mysql> show variables like '%system_time%'; 
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| system_time_zone | CST   |
+------------------+-------+
1 row in set (0.00 sec)

這個值默認是與操做系統的時區相同
數據庫

查閱官檔:函數

http://dev.mysql.com/doc/refman/5.6/en/time-zone-support.html

在mysqld啓動以後默認由system_time_zone參數控制,而每一個會話的時區設定則由time_zone變量控制spa

mysql> select @@global.system_time_zone,@@time_zone;
+---------------------------+-------------+
| @@global.system_time_zone | @@time_zone |
+---------------------------+-------------+
| CST                       | SYSTEM      |
+---------------------------+-------------+
1 row in set (0.00 sec)

因爲此時的系統時區是CST=GMT+8,因此把time_zone替換成+00:00就是GMT了
操作系統

mysql> set time_zone='+00:00';   
Query OK, 0 rows affected (0.00 sec)

mysql> select from_unixtime(1406107120);
+---------------------------+
| from_unixtime(1406107120) |
+---------------------------+
| 2014-07-23 09:18:40       |
+---------------------------+
1 row in set (0.00 sec)

至於from_unixtime與unix_timestamp的差異在另外一篇博客中記錄。unix

相關文章
相關標籤/搜索