您好,登錄后才能下訂單哦!
這篇文章給大家介紹在MySQL數據庫中利用timestamp怎么跳過時區(qū),內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
開始
查看當前數據庫時區(qū)
mysql> show variables like "%time_zone%"; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | system_time_zone | CST | | time_zone | +08:00 | +------------------+--------+ 2 rows in set (0.30 sec)
查看表結構
mysql> desc timestamp_test; +--------------+-----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+-----------+------+-----+---------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | created_time | datetime | YES | | NULL | | | created_at | timestamp | YES | | NULL | | +--------------+-----------+------+-----+---------+----------------+ 3 rows in set (0.26 sec)
插入數據
mysql> insert into timestamp_test(created_time, created_at) values('2020-12-09 08:00:00', '2020-12-09 08:00:00'); Query OK, 1 row affected (0.22 sec) mysql> select * from timestamp_test; +----+---------------------+---------------------+ | id | created_time | created_at | +----+---------------------+---------------------+ | 1 | 2020-12-09 08:00:00 | 2020-12-09 08:00:00 | +----+---------------------+---------------------+ 1 row in set (0.06 sec)
這個時間看起來是沒問題的, 那么我們嘗試修改時區(qū)再插入數據
mysql> SET time_zone = "+00:00"; Query OK, 0 rows affected (0.03 sec) mysql> insert into timestamp_test(created_time, created_at) values('2020-12-09 08:00:00', '2020-12-09 08:00:00'); Query OK, 1 row affected (0.03 sec) mysql> SET time_zone = "+08:00"; Query OK, 0 rows affected (0.04 sec)
這時候再查看數據, 兩條插入的SQL
是一樣的,但是發(fā)現(xiàn)查詢的結果是不一樣的
這兩條數據created_at
的相差正好是時區(qū)的時間差
mysql> select * from timestamp_test; +----+---------------------+---------------------+ | id | created_time | created_at | +----+---------------------+---------------------+ | 1 | 2020-12-09 08:00:00 | 2020-12-09 08:00:00 | | 2 | 2020-12-09 08:00:00 | 2020-12-09 16:00:00 | +----+---------------------+---------------------+ 2 rows in set (0.06 sec)
再看一下實際存儲的時間戳, 然后我們變化時區(qū), 發(fā)現(xiàn)字段時間變化了,但是原始的時間戳數據沒變
mysql> select *, unix_timestamp(created_at) from timestamp_test; +----+---------------------+---------------------+----------------------------+ | id | created_time | created_at | unix_timestamp(created_at) | +----+---------------------+---------------------+----------------------------+ | 1 | 2020-12-09 08:00:00 | 2020-12-09 08:00:00 | 1607472000 | | 2 | 2020-12-09 08:00:00 | 2020-12-09 16:00:00 | 1607500800 | +----+---------------------+---------------------+----------------------------+ 2 rows in set (0.06 sec) mysql> SET time_zone = "+00:00"; Query OK, 0 rows affected (0.09 sec) mysql> show variables like "%time_zone%"; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | system_time_zone | CST | | time_zone | +00:00 | +------------------+--------+ 2 rows in set (0.08 sec) mysql> select *, unix_timestamp(created_at) from timestamp_test; +----+---------------------+---------------------+----------------------------+ | id | created_time | created_at | unix_timestamp(created_at) | +----+---------------------+---------------------+----------------------------+ | 1 | 2020-12-09 08:00:00 | 2020-12-09 00:00:00 | 1607472000 | | 2 | 2020-12-09 08:00:00 | 2020-12-09 08:00:00 | 1607500800 | +----+---------------------+---------------------+----------------------------+ 2 rows in set (0.18 sec)
因為這一切是MySQL
隱式的幫我們轉換了, 讓我們不用關心時區(qū)的問題
就是數據庫實際上會保存 UTC 時間戳,寫入的時候先按 Session 時區(qū)轉成 UTC 時間,讀出的時候再按 Session 時區(qū)轉成當前時區(qū)的時間,這些轉換都是透明的
2020-12-09 08:00:00
時間的一條數據2020-12-09 08:00:00
MySQL
,并且把當前連接的時區(qū)設置為+00:00
,再去查數據庫這條記錄,查到的數據是:2020-12-09 00:00:00
, 正好對應零時區(qū)的時間,這樣子我們就不用考慮時區(qū)的問題.關于在MySQL數據庫中利用timestamp怎么跳過時區(qū)就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。