標籤:style http os 資料 2014 re
開發人員執行如下SQL
[email protected]{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> select from_unixtime(1406107120);+---------------------------+| from_unixtime(1406107120) |+---------------------------+| 2014-07-23 17:18:40 |+---------------------------+1 row in set (0.00 sec)
返回的時間值相差8個小時,查看當前系統的時間戳記
[email protected]{wm_ztcj} >\! dateWed Jul 23 10:49:09 CST 2014
查看資料庫的時區設定
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變數控制
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的差別在另一篇部落格中記錄。