View Single Post
Old 09-21-2007, 09:58 PM   #4 (permalink)
Salathe
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,377
Thanks: 5
Salathe is on a distinguished road
Default

I'm not sure of the logic on Village Idiot's function (1. Get difference between now and birth date in seconds; 2. Push that into date function, which accepts timestamps, not random seconds; 3. Expect to retrieve a year).

Wildhoney's code, whilst I don't see any problems with the functionality, is a wee bit of a mess (mostly because I never really liked elseifs). The code can be squished into the following function which behaves identically to Wildhoney's but, I think, is a little neater.
PHP Code:
function getAgeSalathe($iTimestamp)
{
    
// See http://php.net/date for what the first arguments mean.
    
$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 || ($iDiffMonth == && $iDiffDay 0))
    {
        
$iDiffYear--;
    }
        
    return 
$iDiffYear;

Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
maZtah (01-10-2009)