Correct way to store MySQL date after year 2037

一世执手 提交于 2019-12-22 10:00:00

问题


I am trying to store in MySQL a date() field a successive date to the year 2037.

For example: 2065-12-01

Problem is that the field is returning: 1969-12-31

What is the correct way to record these values on DB? Should I use VARCHAR?

I compute the date field's value like this:

 $future_date = date('Y-m-d', strtotime("+$number_years_to_add years"));

回答1:


You probably use a timestamp field to store the dates and not a datetime field.

See mysql documentation on datetime data types, specifically:

The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.

The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.

The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.




回答2:


It looks to me like you're using the php date and strtotime stuff on a 32-bit platform. Those functions turn into a pumpkin, like Cinderella's carriage, at 03:14:07 UTC on 19 January 2038.

You may wish to check out the new php DateTime class, or upgrade to a 64-bit php implementation.

Also, read this. Accessing dates in PHP beyond 2038

(Thanks for catching this problem with 22 years of advance notice. Others will not.)



来源:https://stackoverflow.com/questions/33802337/correct-way-to-store-mysql-date-after-year-2037

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!