Jump to content

How best to store dates for future calculations?


dmccabe

Recommended Posts

I havent done any php in a long time and I was never an expert at it when I did.

 

I remember having particular problems with dates, calculations  and storing dates in uk formats.

 

So I am basically making an asset database and want to record things like date of purchase.  Warranty expiration date etc.

 

I will then want to create a report screen later that will allow us to view all items where the warranty expiration is due in the next X amount of months for example.

 

The dates will be input on a form basis and in uk date format: DD/MM/YYYY

 

 

So what type of field should I be using in the database to make this process easier?

I have read allot of other threads on this, and everyone likes to do things differently.

 

Personally, I always store dates as a timestamp in the database, then convert them back using either the date() or the gmdate() php function.

 

Being UK like me you will probably want to use gmdate() to get a Greenwich Mean Time date.

Store them using a DATE field. If you also require the time use a DATETIME.

These field types make it much easier to write SQL to produce your reports allowing you to group by dates, etc. If you were to use a unix timestamp then querying is more difficult as you have to convert the timestamp back to date with FROM_TIMESTAMP.

 

Dates will be formatted YYYY-MM-DD however it is so easy to convert them to UK format using your application code.

If you're going to be doing any calculations, I would store them as a timestamp. Otherwise date is fine.

How is it any different?

 

I've always had to convert any dates into a timestamp before I could calculate anything based on that date without writing a lot of code. You have way more experience than me, but I just like to keep it simple. I'm sure you can do it both ways. I've just never seen a simple way of doing it without a timestamp.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.