Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: calculating age

  1. #1
    Regular Coder
    Join Date
    Feb 2006
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question calculating age

    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.

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Feb 2006
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?????

  • #4
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts

    Post

    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:
    PHP Code:
    //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
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #5
    Regular Coder
    Join Date
    Feb 2006
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    actually is i'm not really know hot to do it.... 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

  • #6
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    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
    Last edited by whizard; 01-05-2007 at 05:20 PM.
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #7
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,633
    Thanks
    4
    Thanked 148 Times in 139 Posts
    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.

  • #8
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,011
    Thanks
    2
    Thanked 312 Times in 304 Posts
    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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #9
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    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
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #10
    Regular Coder
    Join Date
    Feb 2006
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by guelphdad View Post
    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....

  • #11
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Here is the tutorial the guelphdad was talking about:
    http://dev.mysql.com/doc/refman/4.1/...culations.html

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

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #12
    Regular Coder
    Join Date
    Feb 2006
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hurray!!!!finally i get the age dee....thanks to everyone

  • #13
    Regular Coder
    Join Date
    Feb 2006
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #14
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    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
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #15
    Regular Coder
    Join Date
    Feb 2006
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    er....not really how to do it...... 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


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •