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

Link to comment
Share on other sites

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

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.