4

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"));
O. Jones
  • 103,626
  • 17
  • 118
  • 172
Hid Dencum
  • 582
  • 4
  • 21

2 Answers2

5

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.

Shadow
  • 33,525
  • 10
  • 51
  • 64
3

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.)

Community
  • 1
  • 1
O. Jones
  • 103,626
  • 17
  • 118
  • 172