Jump to content


Photo

persons age using DOB


  • Please log in to reply
3 replies to this topic

#1 robcrozier

robcrozier
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 18 April 2006 - 05:40 PM

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!

#2 bbaker

bbaker
  • Members
  • PipPipPip
  • Advanced Member
  • 127 posts
  • LocationNY

Posted 18 April 2006 - 06:23 PM

found this on the Zend site when looking up PHP +"age calculation" on Google:

<?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 
?>


#3 lead2gold

lead2gold
  • Members
  • PipPipPip
  • Advanced Member
  • 164 posts
  • LocationOttawa, On

Posted 18 April 2006 - 06:31 PM

[!--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) View Post[/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]
$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! :) 


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

#4 ypirc

ypirc
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 18 April 2006 - 07:55 PM

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".

<?
$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);

?>

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users