Reputation: 1058
I'm trying to show my members age in profile page. in php file i just added this code to show users birth day:
$data = $db->super_query( "SELECT dob FROM dle_users" );
$tpl->set("{bdate}", $data['dob']);
and in profile page i have this result:
1364-07-09
Its a Persian's date: yyyy-mm-dd
now i need to show members age, i searched in google and this site, i found some answers and tips, but in wont work for me! i don't know why.
how i can show age? :(
Upvotes: 3
Views: 1823
Reputation: 125855
You can use MySQL's TIMESTAMPDIFF()
function:
SELECT TIMESTAMPDIFF(YEAR, dob, CURRENT_DATE) AS age FROM dle_users
Upvotes: 6
Reputation:
Just for variety:
select year(now()) - year(dob) - if(dayofyear(now()) < dayofyear(dob), 1, 0) from dle_users
Upvotes: 1
Reputation: 6700
You can use
SELECT DATEDIFF(dob, NOW()) AS difference from dle_users;
This will give you the difference in date which you can convert to various formats using PHP
date("m/d/y g:i (A)", $DB_Date_Field);
Also you can directly use the TIMESTAMPDIFF as:
SELECT TIMESTAMPDIFF(YEAR, dob, CURDATE()) as difference from dle_users;
Read http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timestampdiff
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_curdate
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff
Upvotes: 1
Reputation: 5271
Take a look at MySQL (if that is what you're using) date functions, they'll do a whole host of converting for you.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
Maybe DATEDIFF( NOW(), dob )
or some such malarkey, but that would return the difference in days, or TIMESTAMPDIFF(MONTH, NOW(), dob )
for months
Upvotes: 2