Results 1 to 4 of 4

Thread: [RESOLVED] Calculate the Age

  1. #1

    Thread Starter
    Addicted Member dbasenoob's Avatar
    Join Date
    Jan 2009
    Location
    San Pedro Laguna - Philippines
    Posts
    206

    Resolved [RESOLVED] Calculate the Age

    How can i get my exact age? i make a code but it doesn't work. i want to happen is calculate the age. date now - date of birth then will show my age.

    Code:
    <?php
    $day = $_POST['day'];          //using select or drop down list
    $month = $_POST['month'];   //using select or drop down list
    $year = $_POST['year'];       //using select or drop down list
    $age = date(("m-d-Y") - ($month.$day.$year));
    
    echo "<h1>Your age now is $age </h1>"; 
    ?>

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Calculate the Age

    first, use mktime() to create a Unix timestamp.

    PHP Code:
    $birthdate mktime(000, (int) $_POST['month'], (int) $_POST['day'], (int) $_POST['year']); 
    then, subtract the years. if you're only interested in the year:

    PHP Code:
    echo "you are " . (date('Y') - date('Y'$birthdate)) . " years old."
    alternatively, if you are not storing the $birthdate variable in a database or something, you could make it even more simple:

    PHP Code:
    echo " you are " . (date('Y') - (int) $_POST['year']) . " years old."

  3. #3

    Thread Starter
    Addicted Member dbasenoob's Avatar
    Join Date
    Jan 2009
    Location
    San Pedro Laguna - Philippines
    Posts
    206

    Re: Calculate the Age

    thanks it works... but how could i use the day and month? let say my birthday is august 25 1988 and my current age is 21. when august 25 past my age will be 22? how could i code it?

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Calculate the Age

    the first solution in my post that makes use of mktime() will take the day and month of the year into account. the second method only deals with the year.

Posting Permissions

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



Click Here to Expand Forum to Full Width