Mysql 无效的日期时间格式:1292 不正确的日期时间值

Maw*_*awg 2 mysql datetime

mysql> describe taps;
+-------------+-------------+------+-----+-------------------+-------+
| Field       | Type        | Null | Key | Default           | Extra |
+-------------+-------------+------+-----+-------------------+-------+
| tag_id      | int(11)     | NO   |     | NULL              |       |
| time_stamp  | timestamp   | NO   |     | CURRENT_TIMESTAMP |       |
| device_id   | varchar(45) | YES  |     | NULL              |       |
| device_type | varchar(45) | YES  |     | NULL              |       |
+-------------+-------------+------+-----+-------------------+-------+

mysql> INSERT INTO `taps` (tag_id, time_stamp) VALUES(0, 1451610061);
ERROR 1292 (22007): Incorrect datetime value: '1451610061' for column 'time_stamp' at row 1
Run Code Online (Sandbox Code Playgroud)

为什么??我发现了许多类似的问题,但没有一个看起来像黑白分明。

1451610061 一个有效的时间戳。我在http://www.unixtimestamp.com/ 上检查了它,它按预期评估。

那么,为什么 MySql 不喜欢它呢?

Ada*_*ley 5

MySQLtimestamp格式是2016-02-13 15:48:29 或先将Y-m-d H:i:s 您的格式转换timestamp为该格式,然后 MySQL 将接受它。

如果您在没有定义时间戳的情况下插入新记录,然后从该表中选择行,您会注意到这是它为新default记录提供的格式。

如果您想在查询中直接转换它,请使用:

INSERT INTO `taps` (tag_id, time_stamp) VALUES(0, from_unixtime('1451610061'));
Run Code Online (Sandbox Code Playgroud)

使用StackOverflow 上的此问答作为参考。和from_unixtime 文档