1

I am making a blog cms system and the release date and time of the posts should be shown on my site.

Should I save the Datetime in the php code with a function and save it in a varchar in the database or should I use the default value of the database by using DATETIME.

Which is the more professional way to do this / The more efficient way

  • 2
    Use DATETIME, no question! – Vérace Aug 10 '19 at 23:37
6

You should always store dates using the DATE type. It should use less storage and will generally be faster when searching than if you stored the same date as a VARCHAR.

Additionally, if you stored it as VARCHAR then the format is baked into your data, which will make it harder for you to use different date formats in the presentation layer. For example, what if one user uses en_US and one uses en_GB, the date will get stored differently within the same column of your database. Then when you query your data, how will you know whether '01/12/2020' is January 12th 2020 or December 1st 2020? There is a big difference.

Storing the date as a date allows you to apply different formatting (such as language differences, or precision differences) in the presentation layer where it belongs.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.