robcrozier Posted April 18, 2006 Share Posted April 18, 2006 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! Link to comment https://forums.phpfreaks.com/topic/7748-persons-age-using-dob/ Share on other sites More sharing options...
bbaker Posted April 18, 2006 Share Posted April 18, 2006 found this on the Zend site when looking up PHP +"age calculation" on Google:[code]<?phpfunction 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] Link to comment https://forums.phpfreaks.com/topic/7748-persons-age-using-dob/#findComment-28269 Share on other sites More sharing options...
lead2gold Posted April 18, 2006 Share Posted April 18, 2006 [!--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. Link to comment https://forums.phpfreaks.com/topic/7748-persons-age-using-dob/#findComment-28272 Share on other sites More sharing options...
ypirc Posted April 18, 2006 Share Posted April 18, 2006 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.phpYou are 39 years, 11 months, 24 days old[/quote] Link to comment https://forums.phpfreaks.com/topic/7748-persons-age-using-dob/#findComment-28301 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.