Determining an age with preset variables

Discussion in 'PHP' started by audax, Jan 25, 2007.

  1. #1
    What I'm trying to do is determine a person's age if I have something like the following:

    
    $year = "1964";
    $month = "04";
    $day = "05";
    
    PHP:
    Now that I've got these variables, how do I compare them to today's date in order to get the age? I know I'd have to use the date() function somehow, I'm just not sure how.
     
    audax, Jan 25, 2007 IP
  2. picouli

    picouli Peon

    Messages:
    760
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    0
    #2
    No, date() is not useful for what you need. mktime [ http://php.net/mktime ] is your friend today.

    Have a try and let us know if you need more help
     
    picouli, Jan 25, 2007 IP
  3. rays

    rays Active Member

    Messages:
    563
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #3
    try this snippet, i had used it in past.... seems to be working fine for me ..

    <?
    $year = "1981";
    $month = "04";
    $day = "11";
    function getdays($day1,$day2)
    {
      return round((strtotime($day2)-strtotime($day1))/(24*60*60),0);
    }
    
    $begin = "$year/$month/$day";
    $end = date("Y/m/d"); // we set today as an example
    echo getdays($begin,$end)/365;
    ?>
    PHP:
    cheers
     
    rays, Jan 25, 2007 IP
  4. zurc

    zurc Banned

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    yeah that might seem to work
     
    zurc, Jan 25, 2007 IP
  5. audax

    audax Peon

    Messages:
    83
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I apologize for bumping this thread, but I need a little more advice and help in this area.

    I'm using the function above to get the ages, but for some reason it is _sometimes_ off by one year. Is there a timezone that I need to set or is mktime more accurate? How can I get it to output the correct ages?
     
    audax, Apr 26, 2007 IP