## Php: Calculating Age

• 1,839 Views
• 12 Replies
Pilot-Doofy
Pilot-Doofy
• Member since: Sep. 13, 2003
• Offline.
Forum Stats
Member
Level 37
Musician
Php: Calculating Age Oct. 4th, 2006 @ 06:57 PM

PHP: Main

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? ;) 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. ;) 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. ;)

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?

Mister-Mind
Mister-Mind
• Member since: Jul. 1, 2006
• Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to Php: Calculating Age Oct. 4th, 2006 @ 07:45 PM

Very interesting!

Momo-the-Monkey
Momo-the-Monkey
• Member since: Oct. 15, 2005
• Offline.
Forum Stats
Member
Level 45
Musician
Response to Php: Calculating Age Oct. 4th, 2006 @ 08:02 PM

Very cool feature. I'd love to have a use for this ;)

But trust me, I'll find a way to use it lol :p

But really, Great job Pilot....that's an awsome tutorial...It helped me a lot!

Hello, from the past!
[ PHP: Main ]

• Member since: Sep. 14, 2006
• Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to Php: Calculating Age Oct. 4th, 2006 @ 08:12 PM

great job, it was very well written. i cant wait for any more tutorials that you work on in the future and will make sure to read them all.

Pilot-Doofy
Pilot-Doofy
• Member since: Sep. 13, 2003
• Offline.
Forum Stats
Member
Level 37
Musician
Response to Php: Calculating Age Oct. 4th, 2006 @ 08:24 PM

At 10/4/06 08:12 PM, DeathWars wrote: great job, it was very well written. i cant wait for any more tutorials that you work on in the future and will make sure to read them all.

Wow, I feel privledged that your first post was a response to my tutorial.

• Member since: Sep. 14, 2006
• Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to Php: Calculating Age Oct. 4th, 2006 @ 09:35 PM

well this isnt my first post on NG but i dont use the TriRift account because i mean i dont do any thing for TriRift any more so this is my independent account that i decided to make plus you my friend doofus are a god among men at coding :)

Zendra
Zendra
• Member since: Sep. 7, 2003
• Offline.
Forum Stats
Moderator
Level 50
Blank Slate
Response to Php: Calculating Age Oct. 5th, 2006 @ 04:27 AM

I always love how you make this detailed and well described tutorials. Doofy's back with his tutorials! :)

Nice work, bro.

NG BBS & Review moderator // PM your randomness and other ramble!

elbekko
elbekko
• Member since: Jul. 23, 2004
• Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to Php: Calculating Age Oct. 5th, 2006 @ 10:59 AM

Looks like a great tutorial =)
Great job :D

I should really make another one sometime...

"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

DannyIsOnFire
DannyIsOnFire
• Member since: Apr. 14, 2005
• Offline.
Forum Stats
Member
Level 21
Movie Buff
Response to Php: Calculating Age Oct. 5th, 2006 @ 12:05 PM

At 10/5/06 10:59 AM, elbekko wrote: Looks like a great tutorial =)
Great job :D

I should really make another one sometime...

Agreed & Agreed.

|| Portfolio || Facebook || Twitter ||

Pilot-Doofy
Pilot-Doofy
• Member since: Sep. 13, 2003
• Offline.
Forum Stats
Member
Level 37
Musician
Response to Php: Calculating Age Oct. 5th, 2006 @ 10:16 PM

Thanks for all the support guys.

henke37
henke37
• Member since: Sep. 10, 2004
• Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to Php: Calculating Age Oct. 6th, 2006 @ 03:22 AM

You do not need that fancy negative trick, the math will work fine with a normal subtraction, the result isn't a time stamp anyhow, so it doesn't matter if the value when interperted is in the future. It is somebodys age in secounds, not from the epocj, but from the instant when they where born

Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.

Pilot-Doofy
Pilot-Doofy
• Member since: Sep. 13, 2003
• Offline.
Forum Stats
Member
Level 37
Musician
Response to Php: Calculating Age Oct. 6th, 2006 @ 07:18 AM

At 10/6/06 03:22 AM, henke37 wrote: You do not need that fancy negative trick

Actually I tested it on various servers throughout the years and I've gotten many different results for not checking if it's negative.

urbn
urbn
• Member since: Jun. 10, 2007
• Offline.
Forum Stats
Member
Level 18
Programmer
Response to Php: Calculating Age Oct. 4th, 2007 @ 08:03 AM

That's really nice.

For leap years, can't you just do 60*60*24*365.25 ?