Closed Thread Icon

Topic awaiting preservation: PHP: calculating age from DoB. (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=27441" title="Pages that link to Topic awaiting preservation: PHP: calculating age from DoB. (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: PHP: calculating age from DoB. <span class="small">(Page 1 of 1)</span>\

 
GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

posted posted 02-04-2006 11:17

hello,
i've got a person's date of birth in the form YYYY-MM-DD and i want to calculate his current age.
does anyone know an easy solution to this? i tried substracting it from date("Y-m-d") which didnt seem to work.

thanks!

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 02-04-2006 14:29

of course not, both are strings - and you'd skip days on leap years and such.

Your best bet is probably to convert both dates into a timestamp, substract, and then
divide by the number of seconds in a day.

Of course you'll run into problems if somebody's born before 1970...

GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

posted posted 02-04-2006 14:56

thanks TP,

so if i got the time difference in seconds, i divide by seconds a year, ill get the age? (leap years ignored, cause it doesnt have to be very exact.)

and yes there are people born before 1970.

edit: i am doing this now:

code:
$age = strtotime(date("Y-m-d")) - strtotime($dob);
$age = round( $age/30758400);



it seems to be off by eight days... probably because of leap years. but that's fine with me



(Edited by GRUMBLE on 02-04-2006 16:28)

(Edited by GRUMBLE on 02-04-2006 16:28)

DL-44
Lunatic (VI) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 02-04-2006 17:22

I would do something along the lines of -


1) subtract the year of the dob from the current year.

2) compare the month/day of the dob to the current date

3) if the day/month on the dob is less than the current day/month, subtract 1 from result of step (1).

Or are you actually looking for the age down to the day?

GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

posted posted 02-04-2006 17:27

that is also a good idea. i will try that.

what i am looking for is to display the age of the person.
so if a person's dob is 1996-02-05, then the script would display
age: 9

if the script is run tomorrow, it should display
age: 10

but as i already said, its no big deal, if it is off by a couple of days.

GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

posted posted 02-04-2006 17:41

yepp, thanks again DL. that seems to be a pretty solid solution.

here is the code in case someone is interested

code:
$y = explode("-", $dob);		
			$year = date("Y") - $y[0];
			if (date("m") >= $y[1])
				if (date("d") >= $y[2])
					$year++;



Arthurio
Paranoid (IV) Inmate

From: the dungeons, corridor 13, cell 3736
Insane since: Jul 2003

posted posted 02-04-2006 18:11

http://www.hawkee.com/snippet.php?snippet_id=440

GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

posted posted 02-04-2006 18:15

nice, but as it says on the page, it will only work from 1970 through 2038, that's why i will stick with my current solution.

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 02-20-2006 06:06

Not that it will help, but here's what I'm using in the upcoming MichiganOffenders.org site:

function getAge($birthday){
$newbday = substr($birthday,6) . substr($birthday,0,2) . substr($birthday,3,2);
$age = floor((date('Ymd') - $newbday)/10000);
return $age;
}

poi
Paranoid (IV) Inmate

From: Norway
Insane since: Jun 2002

posted posted 02-20-2006 09:00

Same here and it works well. If one of the date is known you can even write directly as a numeral, i.e:

code:
<? echo floor( (date('Ymd')-19780222)/10000 )  ?>



« BackwardsOnwards »

Show Forum Drop Down Menu