Page 1 of 1

Use datetime to find age?

Posted: Thu Apr 01, 2010 7:10 pm
by hwdesign
I want to find out how "fresh" an entry is, selecting only entries added 30 days from the current date.

I have a column called imagedate and the values are stored as datetime ( 0000-00-00 00:00:00 ). How could I write a query that will choose entries that are no older than 30 days?

Thanks for your help!

Re: Use datetime to find age?

Posted: Thu Apr 01, 2010 7:39 pm
by Eran
There are several ways to do this. You use date/time functions such as DATE_SUB() or subtract using math operators. I usually opt for the latter since it is easier to use indexes. Something like:

Code: Select all

 WHERE imagedate > NOW() - INTERVAL 30 DAY 

Re: Use datetime to find age?

Posted: Sat Apr 03, 2010 6:41 pm
by hwdesign
This worked perfectly. Thanks!