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.
Results 1 to 6 of 6

Thread: Age Calculation

  1. #1
    Regular Coder
    Join Date
    Mar 2007
    Location
    UK
    Posts
    151
    Thanks
    9
    Thanked 2 Times in 2 Posts

    Age Calculation

    Hey, Ive found a script on the internet which takes a date of birth and changes it to an age. How would I change the code so that the dob is dynamic using $_POST from user input?

    Here is the code:
    PHP Code:
    <?php
    $dob 
    "22-04-1989";
    $date_now date("d-m-Y");
     
    $ex explode("-"$dob);
    $ex1 explode("-"$date_now);
     
    $age $ex1[2] - $ex[2];

    if(
    $ex[1] > $ex1[1])
    {
        
    $age--;
     
    } elseif(
    $ex[1] == $ex1[1] && $ex[0] > $ex1[0])
    {
        
    $age--;
    }
    echo 
    $age;
    ?>
    thanks hope you understand what i mean

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    PHP Code:
    <?php
    $dob 
    $_POST['your name attribute from form'];
    $date_now date("d-m-Y");
     
    $ex explode("-"$dob);
    $ex1 explode("-"$date_now);
     
    $age $ex1[2] - $ex[2];

    if(
    $ex[1] > $ex1[1])
    {
        
    $age--;
     
    } elseif(
    $ex[1] == $ex1[1] && $ex[0] > $ex1[0])
    {
        
    $age--;
    }
    echo 
    $age;
    ?>
    PS: there are some checking you must do before, I explain only how to pass parameters from form

    best regards
    Last edited by oesxyl; 12-30-2007 at 12:43 AM.

  • #3
    Regular Coder
    Join Date
    Mar 2007
    Location
    UK
    Posts
    151
    Thanks
    9
    Thanked 2 Times in 2 Posts
    thanks but I forgot 2 mention 1 thing sorry :s. Theres 3 field elements 1 for day, 1 for month and 1 for year. How would I do it getting data from 3 fields?

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by tyanque View Post
    thanks but I forgot 2 mention 1 thing sorry :s. Theres 3 field elements 1 for day, 1 for month and 1 for year. How would I do it getting data from 3 fields?
    same way for each field, but change the name in the $_POST variable,

    Code:
    <form method="post" action="age.php">
    <input type="text" name="month"/>
    <input type="text" name="day"/>
    <input type="text" name="year"/>
    <input type="submit" name="ok" value="OK"/>
    </form>
    age.php
    PHP Code:
    $day $_POST['day'];
    $month $_POST['month'];
    $yesr $_POST['year']; 
    as in previos post no checking

    A better way is to put all in one file, see example #3 from this link:

    http://www.php.net/manual/en/languag...s.external.php

    best regards

  • Users who have thanked oesxyl for this post:

    tyanque (12-30-2007)

  • #5
    Regular Coder
    Join Date
    Mar 2007
    Location
    UK
    Posts
    151
    Thanks
    9
    Thanked 2 Times in 2 Posts
    thanks ill take a look into that fingers crossed it will work

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by tyanque View Post
    thanks ill take a look into that fingers crossed it will work
    if you have problems, just post them here with details,
    And read the security section from the manual, it's critic.

    http://www.php.net/manual/en/

    best regards


  •  

    Posting Permissions

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