PHP: Calculate Age from DOB

Support, Snippets & Projects

Moderators: Moderator, Global Moderator

Post Reply
ner0
Hero Member
Hero Member
Posts: 982
Joined: Sat May 22, 2004 9:45 am

PHP: Calculate Age from DOB

Post by ner0 »

Hey guys, since I've spent a fair few hours working on this little snippet, I thought it'd be worth sharing.. as I had to search around for some ideas before I settled on my code:

All dates are in YYYY-MM-DD format, $u['DOB'] and &u['tzOffset'] are values taken from a database containing user data, tzOffset is stored as the offset in hours (i.e. -5 / +2 / -3.5)

The final $m['age'] is the value of the age, also included a check to see whether it's a person's actual birthday, incase you'd like to do anything with that.

Ignore the insane comments... they were for me.

Code: Select all

			//-- Calculate Age from DOB, times in YYYY-MM-DD FORMAT! --//
				$dob = $u['DOB'];
	
				# get user timezone... i.e. what we can take away from server time (GMT/UTC)
				$tz = $u['tzOffset'];
	
				# Get date of NOW, unfortunately, server TZ isn't GMT, to make it easier later, convert date INTO GMT here!
				$t = time();
	
					# How far in front/behind (of) GMT are we? (i.e. -0500)
					$gmtDiff = date("O");
	
						# make this figure usable (we need seconds)
						$gmtDiff = ($gmtDiff / 100) * 3600; # here we just turn the -0500 into -5 (-5 hours) then multiply by 3600 (secs per hour =D)
	
				# TRUE GMT TIME NOW?
				$t = $t - $gmtDiff;
	
					# User's Timezone offset? (in seconds)
					$tz = $tz * 60 * 60;
	
				# User's LOCAL TIME?.. hmm, this could be useful globally?.. yup yup.
				$uTime = $t + $tz;
				$m['uTime'] = $uTime;		// worth noting this is global'ed as a unix timestamp

				# Grab both dates as $n and $b (now and birthdate) arrays
				$n = explode("-",date("Y-m-d",$uTime));
				$b = explode("-",$dob);

					# Age step 1, check years..
					$age = $n[0] - $b[0];

					# Are we BEFORE their birth month?
					if ($n[1] < $b[1])
					{
						$age--;
					}

					# Are we IN their birth month?
					else if ($n[1] == $b[1])
					{
						# current month / birth month.. are we before, or on their birthday?
						if ($n[2] < $b[2])
						{
							$age--;
						}
						else if ($n[2] == $b[2])
						{
							$m['bday'] = "y";
						}
					}
					# Otherwise, we've passed.. or are on their birthday. so the original calc of this year minus birth year is correctimondo =]!

			$m['age'] = $age;
There will be much more elegant ways of doing this, I tried using timestamp difference to begin, though it was late and I couldn't think of the impact leap years would make.

Hope this is useful for someone!
[color=\"green\"]'class KPIM::ProcessManager' only defines private constructors and has no friends[/color] <- :( poor class



[18:09:00] * ~Moppy stews ner0 erotically 1 times.
Josh
Hero Member
Hero Member
Posts: 4627
Joined: Fri Jun 04, 2004 7:54 am

PHP: Calculate Age from DOB

Post by Josh »

ROFL! You use comments like I do hahaha. They do work too /biggrin.gif' class='bbc_emoticon' alt=':D' />
[align=center]

Image

“If you stop learning, you stop living.” ~Tami Quiring

“It's the rare man who understands the value of a single perfect rose.”

[/align]
ner0
Hero Member
Hero Member
Posts: 982
Joined: Sat May 22, 2004 9:45 am

PHP: Calculate Age from DOB

Post by ner0 »

I like to use comments to 'vent'... especially when I've been stuck coding strictly to php for so long, it's nice to go insane for a while lol

and for some reason using the 'explode()' command always makes me chuckle /blink.gif' class='bbc_emoticon' alt=':blink:' />
[color=\"green\"]'class KPIM::ProcessManager' only defines private constructors and has no friends[/color] <- :( poor class



[18:09:00] * ~Moppy stews ner0 erotically 1 times.
Post Reply

Return to “PHP &amp; MySQL”