tags:

views:

64

answers:

3

How can I select how many days old a field is based on it's "DateAdded" field?

E.g.:

SELECT DAYSOLD(tbl_stuff.DateAdded) as DaysOld 
  FROM tbl_stuff
+1  A: 

You can use the DATEDIFF function, to get the difference, in days, between tbl_stuff.DateAdded and curdate().


For example :

mysql> select datediff(curdate(), '2010-03-15') as daysOld;
+---------+
| daysOld |
+---------+
|       8 |
+---------+
Pascal MARTIN
Yes, another solution :D
Joe
+2  A: 

Use:

SELECT DATEDIFF(NOW(), ts.dateadded) AS daysold
  FROM TBL_STUFF ts

Reference: DATEDIFF

DATEDIFF() returns expr1 – expr2 expressed as a value in days from one date to the other.

OMG Ponies
THANKS this is it :D
Joe
+1  A: 

You can use to_days() or datediff() functions.

MySQL has plenty of date functions, you can refer directly there, http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html.

Col. Shrapnel
Good answer again :D
Joe