calculate age help !

Discussion in 'PHP' started by Funk-woo10, Apr 10, 2008.

  1. #1
    Hi all,

    I need to calculate a members age and display it like- 34 ect.

    Problem - I have have the members - birth year in one column, birth month, in another and birth day in another, all in one table.

    for example-

    m_year m_month m_day

    How could I calculate their age to display like 34, 21 ect !

    Do i compare it to todays date?
     
    Funk-woo10, Apr 10, 2008 IP
  2. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #2
    
    function birthday ($year, $month, $day)
    {
    $year_diff = date("Y") - $year;
    $month_diff = date("m") - $month;
    $day_diff = date("d") - $day;
    if ($month_diff < 0) $year_diff--;
    elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
    return $year_diff;
    }
    
    echo birthday("1980","7","2");
    ?>
    
    Code (markup):
     
    m0nkeymafia, Apr 10, 2008 IP
  3. singh.ajit05

    singh.ajit05 Peon

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    function checkCurAge($year,$month,$day='') {

    $day=(empty($day))?'1':$day;

    list($this_year, $this_month, $this_day) = explode(' ',date('Y n j'));
    $age = $this_year - $year;

    if ($month>$this_month||($this_month==$month&&$this_day<$day)) {
    $age--;
    }

    return $age;

    }
     
    singh.ajit05, Apr 10, 2008 IP