The Cutting Edge Tech Blog


Calculating age from Date of birth by FSherratt

Calculating age from Date of Birth

This has baffled me for years but a couple of nights ago I finally cracked it. How to accurately work out someones Age from their DOB. For this tutorial I’m using the english method of presenting dates DD-MM-YYYY rather than the american way.

It actually rather simple by comparing the date of birth to the current date you can not only work out their age but also whether it’s their birthday or not.

First we need to collect the date of birth, I imagine most of you store it the same way as me, in a format like this 00-00-0000 so we first need to split this up. We are going to be using the date the 05-08-1994 as an example, to split this up we will be using the php explode function.

$dob = "05-08-1994";
list($day, $month, $year) = explode("-", $dob);
?>

This will give us the individual element of their date of birth. We’ll also need the current date so next we are going to use PHP’s date function to find this out.

$curday = date("d");
$curmonth = date("m");
$curyear = date("Y")
?>

PHP’s date function allows us to calculate pretty much any information you need from days and months to the time accurate to a second. For more information on this you can visit php.net’s date function page.

Now we’ve got all the individual pieces of information we can start to calculate the age. First of all we’re going to find out what the age is roughly. We can do this by taking their birth year from the current year.

$age = $curyear - $year;
$age--;
?>

You may notice that I’ve also used auto decrement on the result from the sum, this is done because the result given is one year two great. So to correct this we use the auto decrement function to reduce the value by 1 digit. Alternatively you could reverse the if function below which would give the same result.

Now we’ve got this value we’re going to work out if their birthday has already been and if so do we need to add another year to their age.

if ( $curmonth > $month ||  $curmonth == $month && $curday >= $day) {
$age++;
}
?>

The if function above works out if an extra year is needed as their birthday has either been or is today. This is achieved by calculating if the birth month is less than the current month or, if the birth month is the same as the current month, if their birthday is greater or equal to the current day.

Now we’ve worked out there age were going to display our result with a simple echo.

<?php
echo $age;
?>

Which prints the result 16! So that’s it a quick and simple method to work out someones age.

As I said earlier you can also work out if it’s their birthday and send them a birthday greeting. You can do this by changing the greater than sign in the final part of the if function to an equals sign. Like so.

<?php
if ( $curmonth == $month && $curday == $day ) {
echo 'happy birthday!';
}
?/>

So that’s it. Thank you for reading!

The full source code is below.

<?php
$dob = "05-08-1994";

	list($day, $month, $year) = explode("-", $dob);

	$curday = date("d");
	$curmonth = date("m");
	$curyear = date("Y");

	$age = $curyear - $year;

	$age--;

	if ( $curmonth > $month || $curmonth == $month && $curday >= $day ) {
		$age++;
	}

	if ( $curmonth == $month && $curday == $day ) {
		echo 'happy birthday!';
	}

echo $age;

?>
Advertisements

Leave a Comment so far
Leave a comment



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s



%d bloggers like this: