Jump to content

[SOLVED] Age from Date of Birth


acp26b

Recommended Posts

Hi,
  I am sure this is pretty easy i just can not figure it out.  I have a from where i submit an age range and it pulls all the clients out of the database within that age range that is accomplished with this:
[code]
        $year = date(Y);
$year1 = $year - $form_age1 ;
$year2 = $year - $form_age2 ;
$monthday = date("m-d");
$date1 = $year1 . "-" . $monthday;
$date2 = $year2 . "-" . $monthday;


        $searchquery = "SELECT c.clientid, c.lname, c.fname, c.dateofbirth FROM clients c WHERE  DATE(`DateOfBirth`) BETWEEN '$date2' and '$date1' and c.active = 'y' ";


[/code]

so then i have all the clients within a cretin age range and i have their birthday that i store in a variable called $db_dateofbirth, when i display the the list of results i want to display their age not their date of birth, how would i go about deriving the age? when i have todays date [ $today = date("yyyy-mm-dd") ] and their date of birth in the same format?
Link to comment
Share on other sites

you might want to convert the two time vars into unix timestamps using mktime() function, then you can subtract the DOB from the current date, then use strftime() function to format the output.

$unix_dob = mktime(0,0,0,$month,$day,$year);
$today = time();

$diff = $today - $unix_dob;
$age = strftime('%y-%j', $diff);

would output something like 10 years - 122 days
Link to comment
Share on other sites

Or, I like this one (more along the lines of the first suggestion):
[code]
<?php
$bday = "2/15/1980";
$diff = abs(time() - strtotime($bday));
$age  = floor($diff / (60 * 60 * 24 * 365));
?>
[/code]

Keep in mind the usage of floor() versus round(). With floor(), you guarantee you will show the actual age until each year is ended. Now, this is also a very generic way to count since it doesn't take into account any leap years at all. There are some more eloquent ways to handle [b]exact[/b] age matching, but typically, this is good enough for validation purposes since it only gets off one day every four years.
Link to comment
Share on other sites

Thanks for all the fast replies!

SemiApocalyptic, the funtion seems to top out at 37 all the clients that are over 37 return an age of 37, kind of weird and i can not figure out why.

I am in the process of trying dgiberson's suggestion right now (working on tokenizing my date down to $year, $month, $day), when i tried the floor method explained by obsidian it retuned 0 for all ages, i think it has to to with a format issue. 

Thanks again for the replies, this gives me some stuff to work with
Link to comment
Share on other sites

The max age is always going to be 37 [b]this year[/b] since the UNIX timestamp starts with 12am on January 1, 1970. That gets into the more specific targeting I mentioned above. I'm not sure why mine above is giving '0', though, but it's not worth fixing since it's going to have the same limitation as SA's :P
Link to comment
Share on other sites

you can use another class to get ages over 37. it is called adodb_date. It will let you calculate any ages. Download the file then include it and then you can use this
[code]
<?php
include("adodb-time.inc.php");
function age($day, $month, $year){
$today = date("Y");
$born = adodb_date("Y", adodb_strftime(adodb_mktime(0,0,0,$month,$day,$year)));
$age = $today-$born;
return $age;
}


echo age("24", "12", "1945"); // will return 62

?>[/code]

Will have to do a little more code to get it to do a more exact age

Ray

[attachment deleted by admin]
Link to comment
Share on other sites

Maybe this will help:
[code]
<?php
function birthday($birthday){
                list($month,$day,$year) = explode("-",$birthday);
                $year_diff  = date("Y") - $year;
                $month_diff = date("m") - $month;
                $day_diff  = date("d") - $day;
                if ($day_diff > 0 || $month_diff < 0){
                    $year_diff--;
                }
            return $year_diff;
            }
            echo birthday('07-22-1986');  // Returns 20  mm-dd-yyyy
?>
[/code]
Link to comment
Share on other sites

Thanks little guy, worked perfect!

One more newbie question for today, where is the solved button?  I read the thread on top of board and it said to mark them solved when they are, but i think i am either dumb or blind cause i cant find it, neither can firefox (Ctrl +f)


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.