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

it2022-05-05  176

手册中的描述:

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’。


最新回复(0)