Jump to content

Getting a users age from their date of birth


TeddyKiller

Recommended Posts

I tried this function (see below), but this always returns 40.

I figured it's because the DOB in the database isn't a timestamp.. so how could I change this so that it works.

 

function getAge($iTimestamp)
{
    $iDiffYear  = date('Y') - date('Y', $iTimestamp);
    $iDiffMonth = date('n') - date('n', $iTimestamp);
    $iDiffDay   = date('j') - date('j', $iTimestamp);
    
    // If birthday has not happen yet for this year, subtract 1.
    if ($iDiffMonth < 0 || ($iDiffMonth == 0 && $iDiffDay < 0)) {
        $iDiffYear--;
    }
        
    return $iDiffYear;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.