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
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
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 |
+---------+
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.
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.