Convert DOB to Age





1
Date Submitted Fri. Aug. 25th, 2006 3:41 AM
Revision 1 of 1
Helper Cyber-Drugs
Tags ??? | else | function | if | phpcode | return
Comments 0 comments
The below is an old bit of code I threw together when I was developing a Friend Networking site back in 2001. The site didn't last all that long, but I did manage to save all the handy snippets I used in it.

Below is code, which will return the age of somebody, when you parse their date of birth to it. I will probably make a cleaner version of the code at a later date and share it with everyone here.

<?php
//
// Return user's age by entering in DOB
//
function getAge($date_d, $date_m, $date_y)
{       
    $today_d = date("d");
    $today_m = date("m");
    $today_y = date("Y");
   
    if ($date_d <= $today_d)
    {
        $d_d = $today_d - $date_d;
    }
   
    if ($date_m <= $today_m)
    {
        $d_m = $today_m - $date_m;
    }
   
    if ($date_y <= $today_y)
    {
        $d_y = $today_y - $date_y;
    }
   
    $age = $d_y;
    if ($d_m)
    {
        if ($d_m == "0")
        {
            if ($d_d)
            {
                $age = $age - 1;
            }
        }
    } else {
        $age = $age - 1;
    }
    return $age;
}
?>
 

Justin Nel

Comments

There are currently no comments for this snippet.

Voting