954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

age caluculation in php...

I have stored date of birth of my users into my database..
I want to calculate their age according to the database dob field...
Please help me..

Thanks..

Suneetha Reddy
Newbie Poster
13 posts since Jul 2008
Reputation Points: 10
Solved Threads: 1
 
nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 
SELECT DATE_FORMAT( FROM_DAYS( DATEDIFF( NOW( ) , "1978-03-28" ) ) , '%Y' ) +0 AS age

Even this works. Just replace the date (1978-03-28) with the date of birth column.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Hi Naveen...

I have done a code with your reference..
But i got error in mysql:

Error
SQL query:

"SELECT fname,date,curdate(),(YEAR(curdate())-YEAR(date))-(RIGHT(curdate(),5)

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

date is a mysql reserved keyword. You should use `` around reserved keyword fields to make them work.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 
date is a mysql reserved keyword. You should use `` around reserved keyword fields to make them work.

If dont mind...
Please edit my code and resend...
Thank you...

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

replace date with `date`.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

date is my database field, and i declared it as varchar...
Is it a problem?

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

Yes. date is a mysql keyword and whatever the datatype is, its still a problem.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Yep..
I changed my date to date1..
Still i got the error...

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

Then maybe something is wrong with your query.
I tried it and it worked !

SELECT uname,datej,curdate(),(YEAR(curdate())-YEAR(datej))-(RIGHT(curdate(),5)<RIGHT(datej,5)) AS age FROM members ORDER BY uname
nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

i got the correct output...
What my mistake is:
i have declared dob with varchar,after i changed it to date,correct output came...

Thank you naveen....

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

Great! :)

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Hello nav..
In the above code CURDATE is database field or its todays date...tel me.

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 
nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You