...

View Full Version : calculating age



tanhaha_how
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.

GJay
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.

tanhaha_how
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?????

whizard
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")
{
$dob=strtotime($dob);
if((date("m") == date("m",$dob)) && (date("d") < date("d",$dob) ) || (date("m") < date("m",$dob)))
{
$age= ((date("Y")-date("Y",$dob))-1);
}
else
{
$age= (date("Y")-date("Y",$dob));
}
}


HTH,
Dan

tanhaha_how
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:

whizard
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

Dan

guelphdad
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.

CFMaBiSmAd
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.

whizard
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!

Dan

tanhaha_how
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

whizard
01-06-2007, 02:23 AM
Here is the tutorial the guelphdad was talking about:
http://dev.mysql.com/doc/refman/4.1/en/date-calculations.html

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

Dan

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

tanhaha_how
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

whizard
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.

Dan

tanhaha_how
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:

whizard
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.

Dan

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

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

:)

Dan

tanhaha_how
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:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum