Use datetime to find age?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
hwdesign
Forum Newbie
Posts: 9
Joined: Fri Dec 18, 2009 4:40 am

Use datetime to find age?

Post 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!
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Use datetime to find age?

Post 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 
hwdesign
Forum Newbie
Posts: 9
Joined: Fri Dec 18, 2009 4:40 am

Re: Use datetime to find age?

Post by hwdesign »

This worked perfectly. Thanks!
Post Reply