TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-21-2007, 04:11 PM   #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,587
Thanks: 72
Wildhoney is on a distinguished road
Wink Calculating an Age from Date of Birth

If you're particularly nosey then calculating an individual's age from their date of birth can be fascinating. Although there is a way to calculate the age using MySQL alone, MySQL shouldn't really be used as a calculator.

The following function will take a time stamp and output you the user's age.

PHP Code:
function getAge($iTimestamp
{
    
$iAge date('Y') - date('Y'$iTimestamp);
    
    if(
date('n') < date('n'$iTimestamp)) 
    {
        return --
$iAge;
    } 
    elseif(
date('n') == date('n'$iTimestamp)) 
    {
        if(
date('j') < date('j'$iTimestamp)) 
        {
            return 
$iAge 1;
        } 
        else 
        {
            return 
$iAge;
        }
    } 
    else 
    {
        return 
$iAge;
    }

Just remember to feed in a time stamp.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-21-2007, 08:14 PM   #2 (permalink)
The Gregarious
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 593
Thanks: 15
Village Idiot is on a distinguished road
Default

you could just do this
PHP Code:
function bday($day)
{
$age time() - $bday;
$years date("Y",$age);
return 
$years;

Village Idiot is offline  
Reply With Quote
Old 09-21-2007, 08:27 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,587
Thanks: 72
Wildhoney is on a distinguished road
Default

I tried that, Village Idiot, but all I appear to get back is 1991:

PHP Code:
echo bday('10-10-1985');

function 
bday($bday)
{
    
$age time() - strtotime($bday);
    
$years date("Y",$age);
    return 
$years;

__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-21-2007, 10:58 PM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 713
Thanks: 2
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
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 07:57 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0