Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,434 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,487 people online right now. Registration is fast and FREE... Join Now!




Calculating age tutorial

 
Reply to this topicStart new topic

> Calculating age tutorial

Pilot-Doofy
Group Icon



post 21 Oct, 2006 - 04:25 PM
Post #1


In this tutorial we'll look at the following:
1. How to create a timestamp from a date
2. How to find difference between timestamps
3. Account for negative timestamps (dates before 1/1/1970)
4. Calculate someone's age based on a DOB

First, let's look at the functions we'll be using. Please become familiar with these functions before proceeding with the tutorial.
time()
mktime()
floor()

Okay, let's get started! If you have ever used the date() function I'm sure that you know how it works. You give it a formatted date to output and a timestamp (even if you don't manually provide a timestamp the current timestamp is used). Well, the mktime() function is basically the opposite of that.

For instance, if we put this code into a script it would give us the timestamp for 12:00am on October 4, 2006 (today).
mktime(0, 0, 0, 10, 4, 2006);

Now, let me explain how it works in case you didn't remember from the online documentation. The parameters go in this order:
hour, minute, second, month, day, year, (DST)

The first 6 are pretty self explanitory. The 7th parameter is usually left off and just allows you to create timestamps based on DST (daylight savings time). For example, the following would give us the same timestamp from above but would account for DST.

mktime(0, 0, 0, 10, 4, 2006, 1);

As you can see, we set the 7th parameter to 1 (which is true in boolean logic).

I don't think I need to explain the time() function, but if you were absolutely too lazy to read the documentation and you know nothing about PHP, it returns the current UNIX timestamp (starting from January 1, 1970). Hence, if you have a date before January 1, 1970 the timestamp will be negative or not exist (depending on how old the date is). You may think this isn't important but we'll run into problems later if we don't account for it.

Lastly, the floor() function is similar to round() or ceil() except that floor() founds down rather than evenly or upward as round() and ceil() do, respectively.

Alright, now let's look at some code:
<?php
$ageTime = mktime(0, 0, 0, 9, 9, 1919); // Get the person's birthday timestamp
$t = time(); // Store current time for consistency
$age = ($ageTime < 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime;
$year = 60 * 60 * 24 * 365;
$ageYears = $age / $year;

echo 'You are ' . floor($ageYears) . ' years old.';
?>

Okay, let's break it down line by line. The first line stores the timestamp for the DOB you're attempting to get the age of. Secondly we store $t in a variable both for short-hand use and so that we keep the same timestamp throughout the code. (If you get a timestamp at the top of the page and get one at the bottom of the page they could differ depending on how long it took the page to execute.)

The next line is the most important line in the code. Why do we use the ternary operator to check if the $ageTime timestamp is less than 0? Well, by checking if it's less than 0 we can determine if it is a negative value. Remember when I said negative timestamps could cause problems later? wink2.gif Well, we're preventing those problems from happening here!

For example, let's say you have a timestamp of -3600, or 11:00pm December 31, 1969. Well, when we calculate the difference we need to subtract the the age timestamp ($ageTime) from the current timestamp. This works perfectly fine for anyone under the age of 36. wink2.gif Well, if we were to subtract the timestamp for -3600 it would actually add 3600 onto the current timestamp. So technically it would say you're not even born yet, which I'm sure some older folks wouldn't mind hearing. wink2.gif

Eitherway, that's not what we're trying to achieve. How do we fix this? Well, we can simply multiple the timestamp by a -1 which would reverse the number or make it positive. However, we only want to reverse the number IF the timestamp is negative, if it isn't then we leave it alone.

Okay, so that was a mouthfull, might want to reread the previous paragraphs until you have a firm grasp on the concept at hand. Once you do, feel free to procede.

The next steps are pretty easy. We simply store the amount of time in seconds that a year is so that we know what that number is being used for. The logic behind that number is this:

60 seconds (1 minute) x 60 minutes (1 hour, and a TV show lol) x 24 hours (1 day) x 365 days (1 year).

Note that this doesn't account for leap years and the extra day in those years. If you wanted to get really percise (which in most cases is useless) then you can easily find out how many leap years have occurred since that person's DOB and add the appropriate amount of days.

Next we divide the difference in the two timestamps by the $year variable, which splits their age into years.

We're done now, right? Sorry, not just yet.

We want to make this age calculator graceful. To do this, we need to round down the amount of years we get when we divide $ageYears by $year. The reason behind this is that when you tell someone your age and you're actually 22 years and 300 days old, you would still say you're 22. The idea is that you're 22 (or any other age) all the way up until your next birthday, so we want the age calculator to reflect this concept as well.

Okay, so we've used the floor() function to round down the person's age. Now we're all done. You can print the person's age, use it in profiles, or do whatever with it!

Pretty simple, eh?
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

satish4044
*



post 25 Apr, 2007 - 10:24 PM
Post #2
really this tutorial is useful to me thanks for giving this type of good tutorial
Go to the top of the page
+Quote Post

neclon
*



post 5 Mar, 2008 - 10:27 AM
Post #3
I wanted to post an alternative that is somewhat simplified to the way we really do this kind of thing in our head. I needed to account for leap years because I needed accuracy. I also had a note to add that simplifies the above tutorial (since negative timestamps really do not change the outcome at all and the alternative formula given if it's below 0 gives the same result as simply subtracting).

Instead of trying to convert a final timestamp into years and rounding and accounting for leap years I just subtract the Date of birth year from this year and then subtract one more if their birthday has not come up yet. This takes advantage of PHP automatically converting strings to numbers for formulas. It also allows the date of birth input to be a little more dynamic with strtotime (which could be replaced by mktime if one was worried about resource efficiency:

//Calculate someone's age based on a date of birth string
$year = date("Y"); // Get this year as a string, date(string format, [timestamp default: now])
$dobtime = strtotime("04/09/1980"); // Get date of birth as a timestamp
$age = $year - date("Y",$dobtime); // Take advantage of auto conversion to subtract this year from the year of the DOB
$bday = date("m/d/",$dobtime).$year; // Create a timestamp from their month and day of birth combined with this year
if((time()-strtotime())<0) $age = $age - 1; // Compare their bday for this year to current timestamp and subtract one from the year if their birthday has not occurred yet



One note on the above excellent tutorial:
$age = ($ageTime < 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime;

The handling of negative timestamps due to a date of birth being before 1970 is overcomplicating the code. The 2 above calculations are identical in result so there is really nothing different between them:

4 - 1 = 3
4 + (1*-1) = 4+(-1) = 3

4 - (-1) = 5
4 + (-1*-1) - 4+1 = 5

If you subtract a negative number you are adding. That is the desired result since the resultant timestamp needs to be bigger than the timestamp provided by (time()). For 2008 timestamp is 38 years. If you are born earlier than 1970 you are older than that so you need the additive result of subtracting the negative to get the full difference in time.
Go to the top of the page
+Quote Post

neclon
*



post 24 Mar, 2008 - 11:06 AM
Post #4
QUOTE(neclon @ 5 Mar, 2008 - 11:27 AM) *
//Calculate someone's age based on a date of birth string
$year = date("Y"); // Get this year as a string, date(string format, [timestamp default: now])
$dobtime = strtotime("04/09/1980"); // Get date of birth as a timestamp
$age = $year - date("Y",$dobtime); // Take advantage of auto conversion to subtract this year from the year of the DOB
$bday = date("m/d/",$dobtime).$year; // Create a timestamp from their month and day of birth combined with this year
if((time()-strtotime())<0) $age = $age - 1; // Compare their bday for this year to current timestamp and subtract one from the year if their birthday has not occurred yet


Correction to code (forgot to put the the $bday variable into the strtotime argument on the last line):

//Calculate someone's age based on a date of birth string
$year = date("Y"); // Get this year as a string, date(string format, [timestamp default: now])
$dobtime = strtotime("04/09/1980"); // Get date of birth as a timestamp
$age = $year - date("Y",$dobtime); // Take advantage of auto conversion to subtract this year from the year of the DOB
$bday = date("m/d/",$dobtime).$year; // Create a timestamp from their month and day of birth combined with this year
if((time()-strtotime($bday))<0) $age = $age - 1; // Compare their bday for this year to current timestamp and subtract one from the year if their birthday has not occurred yet
Go to the top of the page
+Quote Post

critical
*



post 6 Aug, 2008 - 05:52 AM
Post #5
Cheers I have been stuck for ages trying to suss out how this was done. Great post!!

This post has been edited by critical: 6 Aug, 2008 - 06:03 AM
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 11/22/08 10:11AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month