Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL如何存储时间戳_Mysql_Timestamp - Fatal编程技术网

MySQL如何存储时间戳

MySQL如何存储时间戳,mysql,timestamp,Mysql,Timestamp,我使用的是MySQL 5.7.20和Ubuntu 16.04 我的问题是: mysql> CREATE TABLE test (ts TIMESTAMP NULL); mysql> INSERT INTO test VALUES (1513776043); mysql> SELECT * FROM test; +---------------------+ | ts | +---------------------+ | 0000-00-0

我使用的是MySQL 5.7.20和Ubuntu 16.04

我的问题是:

mysql> CREATE TABLE test (ts TIMESTAMP NULL);

mysql> INSERT INTO test VALUES (1513776043);

mysql> SELECT * FROM test;
+---------------------+
| ts                  |
+---------------------+
| 0000-00-00 00:00:00 |
+---------------------+

如您所见,时间戳未保存。如何在MySQL中存储时间戳?

您需要使用来自_unixtime()的
对其进行转换,因为这是一个unix时间戳

INSERT INTO test VALUES (FROM_UNIXTIME(1513776043));

由于这是一个unix时间戳,因此您需要将它从_unixtime()
转换为

INSERT INTO test VALUES (FROM_UNIXTIME(1513776043));

来自_unixtime的函数取决于服务器的时区,而时间戳不取决于它。例如:设置时区='+00:00';从_UNIXTIME(1513776043)中选择;返回2017-12-20 13:20:43,设置时区=“+03:00”;返回:2017-12-20 16:20:43好的,我明白了。MySQL将时间戳字段存储为整数,并且
从测试中选择UNIX\u时间戳(ts)
不取决于时区。来自unixtime的函数取决于服务器的时区,而时间戳不取决于它。例如:设置时区='+00:00';从_UNIXTIME(1513776043)中选择;返回2017-12-20 13:20:43,设置时区=“+03:00”;返回:2017-12-20 16:20:43好的,我明白了。MySQL将时间戳字段存储为整数,
从测试中选择UNIX\u时间戳(ts)
不取决于时区