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 7 of 7

Thread: PHP Age Script

  1. #1
    Regular Coder Skyzyx's Avatar
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Age Script

    I'm moving along in my learning of PHP. This is the next step.

    I'm looking for a script where I can pass my birthday, birthmonth, and birthyear as parameters and have it either return or echo my current age.

    I'm sure this will have something to do with date() and/or mktime(), but after Googling for a bit, I haven't found anything that I can really work with.

    I could write one in JavaScript, but I'm looking for a server-side solution. I know that in JavaScript, you need to account for leap years. I'm not sure if that would apply in PHP, but I figured I'd throw that out there to anyone who'd be willing to write me something.

    Any help is appreciated!

    Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    It's not that hard at all
    I not got time to show you how to do it, but there a few a few examples at http://uk.php.net/date

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just reinstalled windows and my web server isn't up yet, so I can't test it, but this should work:

    PHP Code:
    <?php

    if ( isset ( $HTTP_GET_VARS['d'] ) && isset ( $HTTP_GET_VARS['m'] ) && isset ( $HTTP_GET_VARS['y'] ) ) {
        
    $d =  isset ( $HTTP_GET_VARS['d'] );
        
    $m =  isset ( $HTTP_GET_VARS['m'] );
        
    $y =  isset ( $HTTP_GET_VARS['y'] );
        
    $bDay mktime 000$m$d$y );
        
    $cTime time ();
        
    $age $cTime-$bDay;
        
    $yr floor $age/(60*60*24*356.25) );
        print 
    "You are $yr years old";
    }

    ?>
    <form method="GET" action="">
    Day: <input type="text" name="d"><br>
    Month: <input type="text" name="m"><br>
    Year (ex. 1972): <input type="text" name="y"><br>
    <input type="submit" value="Calculate">
    </form>
    Last edited by x_goose_x; 01-18-2004 at 06:19 PM.

  • #4
    Regular Coder Skyzyx's Avatar
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you. I was having a heck of a time looking through the documentation for the PHP functions.

    Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A little subnote, the variable $age is the persons age in seconds, that's why I did all those divisions. You could also do some more calculations to find how many months, days, ... old they are.

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Sheffield, UK
    Posts
    552
    Thanks
    0
    Thanked 0 Times in 0 Posts
    just a v minor perfomance note, you may want to replace 60*60*24*365.25 with 31471200.
    "To be successful in IT you don't need to know everything - just where to find it in under 30 seconds"

    (Me Me Me Me Me Me Me Me Me)

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ya, I made a typo, sorry.

    The reason I did it like that was so it is easier to understand.

    In case you want exact figure:

    http://www.google.ca/search?q=days+i...-1&hl=en&meta=


  •  

    Posting Permissions

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