Mysql

关注公众号 jb51net

关闭
首页 > 数据库 > Mysql > mysql时区错误Incorrect datetime value: '1970-01-01 00:00:01'

解决mysql时区问题导致错误Incorrect datetime value: '1970-01-01 00:00:01'

作者:stanting

这篇文章主要介绍了解决mysql时区问题导致错误Incorrect datetime value: '1970-01-01 00:00:01',具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

mysql时区错误Incorrect datetime value: '1970-01-01 00:00:01'

手册中的描述:

A timestamp. The range is ‘1970-01-01 00:00:01’ UTC to ‘2038-01-19
03:14:07’ UTC.

注意末尾的UTC,此为世界协调时,指的是经度0度上的标准时间。

官方文档中描述:

MySQL converts TIMESTAMP values from the current time zone to UTC for
storage, and back from UTC to the current time zone for retrieval.
(This does not occur for other types such as DATETIME.) By default,
the current time zone for each connection is the server’s time. The
time zone can be set on a per-connection basis. As long as the time
zone setting remains constant, you get back the same value you store.
If you store a TIMESTAMP value, and then change the time zone and
retrieve the value, the retrieved value is different from the value
you stored. This occurs because the same time zone was not used for
conversion in both directions. The current time zone is available as
the value of the time_zone system variable.

文中说到,在存储时会根据当前环境的的时区转换为UTC存储。

而环境中的时区为CST中国标准时间,比UTC要早8个小时,常见表示为GMT+0800 (中国标准时间)。

也正是如此,输入的值存储时需要加八个小时再存储,对应过来timestamp的时间范围表示为’1970-01-01 08:00:01’ and ‘2038-01-19 11:14:07’。

1970-01-01 08:00:00 时间由来

在用ng的date过滤服务的时候时间戳0会转换成1970-01-01 08:00:00;

是因为起源于UNIX系统,以Unix诞生的时间为参照确定的,UNIX认为1970年1月1日0点是时间纪元,现代计算机(电话,电子设备)时间以1970 年 1 月 1 日的 08:00为基准,

在时间转换时,或者时间重置时都会出现这个时间值,unix时间戳零点是 1970年1月1日0点整(utc),北京是gmt 8所以变成八点。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文