0

I am looking to store a best before date for a food product in a MySQL database.

Currently I am storing it as text from an input box.

When viewing the food product on a webpage it will need to display the best before date in a format that the user can relate to. (mm/dd/yyyy or dd/mm/yyyy etc.)

Has anyone stored future dates in their database before? Looking for the best way to do this.

Using PHP too.

Cian W
  • 96
  • 1
  • 1
  • 10
  • 2
    Yes, you store them the same as any other dates, usually as a `DATE` / `DATETIME` / `TIMESTAMP` type. – Jonnix Sep 05 '16 at 13:59
  • In PHP you can define a custom date format. You can either parse them and store them as a DATE object, or simply save the string and then parse it using the right format to create a new date object in PHP. – Nicolas Sep 05 '16 at 14:03
  • @Nicolas would you save it as a string? – Drew Sep 05 '16 at 14:04
  • well both ways are equally efficient, saving it as a string is definitly easier but if you want to add an index on the date you should save it as a date object. If you don't know what an index is, it's to make research more efficient, but in this case it's not necessary. Save it as a string and then parse the string once it goes out of the database. You can then use the date object to produce the date format you want to show. – Nicolas Sep 05 '16 at 14:07
  • Parsing simple string date/times is dodgy as there are many formats, some that look identical even if they mean something completely different. Always use the existing types imo. – Jonnix Sep 05 '16 at 14:12

0 Answers0