Jump to content

Archived

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

robcrozier

persons age using DOB

Recommended Posts

hi can anyone help me?

im trying to calculate someones age using their date of birth that has been passed from a database. i need to create some age restrictions for the project im doing. With that im fine! However i jast cant seem to get an accurate age generated!

cheers!

Share this post


Link to post
Share on other sites
found this on the Zend site when looking up PHP +"age calculation" on Google:

[code]<?php

function calculate_age($birth_day, $birth_month, $birth_year) {

     $datestamp = date("d.m.Y", mktime());
     $t_arr = explode("." , $datestamp);
    
     $current_day = $t_arr[0];
     $current_month = $t_arr[1];
     $current_year = $t_arr[2];
    
     $year_dif = $current_year - $birth_year;
    
     if(($birth_month > $current_month) || ($birth_month == $current_month && $current_day < $birth_day))
         $age = $year_dif - 1;
     else
         $age = $year_dif;

     return $age;

}

echo calculate_age('28','03','1973');  // echos 33
?>[/code]

Share this post


Link to post
Share on other sites
[!--quoteo(post=366094:date=Apr 18 2006, 01:40 PM:name=rob-c)--][div class=\'quotetop\']QUOTE(rob-c @ Apr 18 2006, 01:40 PM) [snapback]366094[/snapback][/div][div class=\'quotemain\'][!--quotec--]
hi can anyone help me?

im trying to calculate someones age using their date of birth that has been passed from a database. i need to create some age restrictions for the project im doing. With that im fine! However i jast cant seem to get an accurate age generated!

cheers!
[/quote]
[code]
$day = 14;
$month = 2; // february
$year = 1980;

$bday=mktime(0,0,0,$day,$month,$year);
$now=date("Y-m-d H:i:s", time());

$age =($now - $bday) /3600;

echo "You are ".$age." year(s) old";

Edit: Doh, i was beaten to the answer! :)

[/code]

I havn't tested the code above, but it should work.

Share this post


Link to post
Share on other sites
Unfortunately, unix timestamping (which is what the methods previously stated use) does not work for people who were born before January 1, 1970. I just wrote this quick solutions..it's not perfect and expects the input to be "mm/dd/YYY".

[code]
<?
$date = '05/25/1966';
$dex = explode('/', $date);
$years = date('Y') - $dex[2];
$days = date('d') - $dex[1];
$months = date('m') - $dex[0];

if($months < 0) {
        $years--;
        $months+=12;
}
if ($days < 0) {
        $days+=date('t', mktime(24,0,0,$dex[0],0,2006));
}
printf("You are %s years, %s months, %s days old\n", $years, $months, $days);

?>
[/code]

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
%php -f age.php
You are 39 years, 11 months, 24 days old
[/quote]

Share this post


Link to post
Share on other sites

×

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.