Laravel: Datetime vs Timestamp - Differences

If we take a look at those fields, we can quickly see that there are some differences:

  • timestamp stores time in seconds, while datetime stores it in a specific format (usually YYYY-MM-DD HH:MM:SS)
  • timestamp has a range of 1970-01-01 00:00:01 UTC to 2038-01-19 03:14:07 UTC, while datetime has a range of 1000-01-01 00:00:00 to 9999-12-31 23:59:59, giving you a much wider range of dates. - source
  • timestamp is stored in 4 bytes, while datetime is stored in 8 bytes, which means that timestamp takes up less space
  • timestamp is stored in UTC, while datetime is stored in the timezone of the server
  • datetime can support fractions of a second, while timestamp cannot

So, which one should you use? It depends on your use case. If you need to store dates between 1970-01-01 00:00:01 UTC to 2038-01-19 03:14:07 UTC, then you should use timestamp. This will make the date searchable really quickly.

But if you need to store dates outside of that range, then you should use datetime. Or if you need to have a timezone other than UTC, then you should use datetime.

No comments or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 55 courses (987 lessons, total 46 h 42 min)
  • 77 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials