代码之家  ›  专栏  ›  技术社区  ›  CIRCLE

SQLSTATE[22007]:无效的datetime格式:1292不正确的datetime值:“2019-03-31 01:52:25”

  •  0
  • CIRCLE  · 技术社区  · 5 年前

    我目前收到了来自多个生产数据库的大量错误,我一直在说:

    SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2019-03-31 01:49:45' for column 'created_at' at row 1 
    

    这是我的桌子:

    +------------+--------------+------+-----+-------------------+----------------+
    | Field      | Type         | Null | Key | Default           | Extra          |
    +------------+--------------+------+-----+-------------------+----------------+
    | id         | int(11)      | NO   | PRI | NULL              | auto_increment |
    | log        | varchar(255) | YES  |     | NULL              |                |
    | created_at | timestamp    | YES  |     | CURRENT_TIMESTAMP |                |
    | updated_at | datetime     | YES  |     | NULL              |                |
    +------------+--------------+------+-----+-------------------+----------------+
    

    MySQL版本:

    mysql  Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using  EditLine wrapper
    


    更新:
    我不知道发生了什么,但是我收到了来自错误监视器的很多通知,现在一切都恢复正常了,代码没有任何变化。

    错误开始于: 2019-03-31 01:00:08
    2019-03-31 01:59:03



    好吧,我是葡萄牙人,今天安排了一个全国性的时间变更+1小时,我完全忘了。。。
    我怀疑这些错误是由于我的服务器或MySQL安装与新时间同步滞后造成的。

    我不提这个问题,以防其他人遇到同样的问题

    1 回复  |  直到 5 年前
        1
  •  5
  •   Nick SamSmith1986    5 年前

    因为从那天开始是夏令时,所以凌晨1点到2点之间的所有时间都是无效的。您会发现问题实际上从“01:00:00”开始,到“01:59:59”结束:

    create table t (created_at timestamp null default current_timestamp);
    insert into t values('2019-03-31 00:59:59');
    insert into t values('2019-03-31 01:00:00');
    

    日期时间值不正确:第1行“created\u at”列的“2019-03-31 01:00:00”

    insert into t values('2019-03-31 01:59:59');
    

    datetime值不正确:“2019-03-31 01:59:59”用于第1行的“created\u at”列

    insert into t values('2019-03-31 02:00:00');
    select * from t
    

    输出:

    created_at
    2019-03-31 00:59:59
    2019-03-31 02:00:00
    

    将系统时区更改为没有夏令时的时区可以解决问题。