View Full Version : calculating age

01-04-2007, 10:38 AM
hi,i hav a problem which is regarding the age....how to increase the age automatically every year inside the database???any idea???

thanks in advance.

01-04-2007, 12:05 PM
if you store the date of birth rather than age, you can just do the calculation when you display it, and you don't have to worry about updating anything.

01-04-2007, 04:44 PM
what if i have 2 textbox, 1 is DOB and another 1 is age....but i do not wan to auto display the age....so izzit i need to use the current year to deduct the year fr the DOB?????

01-04-2007, 04:52 PM
Not sure why you don't want to calculate the age from the DOB, it's not that hard...

You can use this code to calculate the age from the DOB:

//Get AGE
$dob = $year."-".$month."-".$day;
if(isset($dob) && $dob!="0000-00-00")
if((date("m") == date("m",$dob)) && (date("d") < date("d",$dob) ) || (date("m") < date("m",$dob)))
$age= ((date("Y")-date("Y",$dob))-1);
$age= (date("Y")-date("Y",$dob));


01-05-2007, 03:32 PM
actually is i'm not really know hot to do it....:o feel like complicated....
if i use ur method....izzit inside the textbox,i jz need to put -->
value="<?php echo $age; ?>" ????or i need to use onclick or others???

then the d,m and Y,izzit standard or depends????

thank u for helping me:thumbsup:

01-05-2007, 06:17 PM
Yeah just put

<?php echo $age; ?>

in the textbox....

Unfortunatly, the code i gave you doesn't work for people born before 1970...

See Here: http://codingforums.com/showthread.php?t=104332

for a script that works on people born before 1970- its the code posted by Fou-Lu - and with that code as well, all you need to do is put <?php echo $age; ?> in your textbox


01-05-2007, 07:35 PM
even better, why not tell people which database you are using and then a solution might be found for you. Also if you are using mysql check the manual for the Tutorial (I think it is section 3). I seem to recall an example there.

01-05-2007, 07:53 PM
The method at the tutorial that guelphdad mentions would work using any programming language for any yyyy-mm-dd formated date string. It just takes the difference in the year yyyy and then compares the mm-dd as strings to determine if it should subtract one (if the birthday has not occurred yet this year.)

The reason this is different than the code you are using whizard is because it is not dependent on the date(...) function.

01-05-2007, 08:22 PM
Ahh.. That makes a lot of sense, I think I'll try it out!

Plus, it'll will be nice to get rid of dependency on the date() function!


01-06-2007, 02:07 AM
even better, why not tell people which database you are using and then a solution might be found for you. Also if you are using mysql check the manual for the Tutorial (I think it is section 3). I seem to recall an example there.

oops,sorry....i'm using mysql....:p

01-06-2007, 02:23 AM
Here is the tutorial the guelphdad was talking about:

It shows you how to use a MySQL query to get an age from the database.


01-06-2007, 03:41 AM
hurray!!!!finally i get the age dee....thanks to everyone:)
:thumbsup: :thumbsup: :thumbsup: :thumbsup: :thumbsup:

01-06-2007, 03:51 AM
but i hav found another prob....which is after i change my PC date to 2008,the existing record still show the previous age...but when i add a new record...it will show the correct age....which means 2 records,with the same DOB,but with different age.....inside my database have a field name CurrentDate to stall the current date...so any other ways to update the current date (i mean the existing record)...

thanks to all again

01-06-2007, 04:39 AM
Don't store CURRENTDATE() in the database, or it will only be the date of when you inserted the row. Just check the DOB against the real CURRENTDATE when you do the query.


01-07-2007, 05:07 AM
er....not really how to do it......:o izzit can write the date("Y-m-d") inside the sql statement when i compare it????

sorry for my stupid question.....

thank u very much:thumbsup: :thumbsup:

01-07-2007, 05:17 AM
If you use this MySQL query:

SELECT name, birth, CURDATE(), (YEAR(CURDATE())-YEAR(birth)) - RIGHT(CURDATE(),5)<RIGHT(birth,5)) AS age FROM tablename;

where the field `birth` is a DATE field, it will return the age of the person. The reason for this is that it will automatically get the current date when you use CURDATE(). You don't need to store that in the database.


01-09-2007, 11:31 AM
oh,okok!!!!i got it dee...really thank u very much:thumbsup: :thumbsup: :thumbsup: :thumbsup:

01-09-2007, 05:04 PM
No Problem!



01-10-2007, 06:06 PM
sorry....the SQL statement is in SELECT....but what if i wan 2 use in INSERT????
i mean the condition to get the age.....is it possible????

thank u again:thumbsup: