Jump to content

Calculate age using PHP based upon DOB


BrianM

Recommended Posts

<?php
$dtBirthDate = "1978-09-13";

$dtToday = date("Y-m-d");

//Calculate difference in seconds

$intDiffSec = strtotime($dtToday) - strtotime($dtBirthDate);

//Calculate no of days

$intDays = $intDiffSec / (60*60*24);

$intYearsAge = (int) ($intDays / 365);

$intDaysAge = $intDays - ($intYearsAge * 365);

echo "Age is : ".$intYearsAge. " Years and ".$intDaysAge." Days" ;

?>

 

something like this should also work...not tested.

 

PS: Leap years are not considered...

Here is how I did it for my forum:

 

$birthMonth = 4;
$birthDay = 24;
$birthYear = 1946;

if($birthYear > date("n")){
$age = (date("Y") - $birthYear); //you have turned your age because your birthmonth is greater then the current month
}else{
//your birthmonth is less then the current month
//Now check if the month we are in is the same as your month...
if($birthMonth == date("n") && $birthDay < date("d")){
	//It's the same month, but the current day is greater then your actual birth day...
	$age = (date("Y") - $birthYear)-1; //havnt turned your age this year so take 1 away from age
}else{
	//You must've had your birthday...
	$age = (date("Y") - $birthYear); //turned your age this year
}
}

Archived

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

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