I have a problem regarding the datediff
MYSQL function, I can use it and it is simple. But I don't understand how to use it to collect differences within the table field. E.g.
I have a column dob
and I want to write a query that will do something like
select dateDiff(current_timeStamp,dob)
from sometable 'here dob is the table column
I mean I want the difference from the current date time to the table field dob
, each query result is the difference, the age of the user.
If I understand your comments on the previous answers, the date-of-birth column is not actually a
DATE
value but a string in the format m/d/y. I strongly recommend you change this; it slows down any date computations you want to do and you risk invalid date values getting entered into the column.I think this is what you need. It uses the
STR_TO_DATE()
function and an algorithm for computing the age from the MySQL documentation:I think this should help
SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(now()) - TO_DAYS(@dateofbirth)), '%Y') + 0;
Note: Give the D.O.B in the correct format, E.g.
YYYY-MM-DD'=> '1991-11-11
If you want, for each user, display the age in years, do
You mean like this?
(Source)
Try this