-3

Possible Duplicate:
Calculate Age in MySQL (InnoDb)

I have created a table and one of its columns is 'dob' (date of birth). It entered the birth date in the table in 0000-00-00 format. But I need to display my users ($id) with their age instead of the 0000-00-00 format.

My current page is displaying the info from my database in this format. It will display the age of different users.

<td align="center"><? echo $rows['age']; ?></td>

Community
  • 1
  • 1
drftorres
  • 227
  • 2
  • 4
  • What units? Years? Days? Months? Age alone is like speed or weight. Units need to accompany it. – Corbin Apr 11 '12 at 19:24
  • Also, you don't specify if the last `00-00` of your date string is `mm-dd` or `dd-mm`. – msanford Apr 11 '12 at 19:29
  • @msanford with MySQL it's safe to assume yyyy-mm-dd. But, 0000-00-0000 is indeed vague. He should have specified the type of the field instead unless it's varchar in which the format of the date would need to be explicity stated. – Corbin Apr 11 '12 at 19:32

1 Answers1

0

Here is a SQL query you can use.

SELECT EXTRACT(YEAR FROM (FROM_DAYS(DATEDIFF(NOW(), '1982-01-01')))) AS age
mdoyle
  • 737
  • 9
  • 22
  • And now having seen the comments above, a caveat: That's assuming the data type of the column dob is date or datetime. – mdoyle Apr 11 '12 at 19:36
  • I placed: `SELECT EXTRACT(YEAR FROM users(FROM_DAYS(DATEDIFF(NOW(), '1982-01-01')))) AS age` but didn't work – drftorres Apr 11 '12 at 23:51
  • Could you elaborate on "but didn't work"? You are using MySQL, right? `root@beren [~]# mysql` `Welcome to the MySQL monitor. Commands end with ; or \g.` `Your MySQL connection id is 13408` `Server version: 5.0.95-community MySQL Community Edition (GPL)` `` `mysql> SELECT EXTRACT(YEAR FROM (FROM_DAYS(DATEDIFF(NOW(), '1982-01-01')))) AS age;` `+------+` `| age |` `+------+` `| 30 |` `+------+` `1 row in set (0.00 sec)` `` `mysql>` Hoping the comment formatting works...and it doesn't seem to be. – mdoyle Apr 12 '12 at 16:06