Page 1 of 1

Calculate Age

Posted: Mon Apr 02, 2007 9:02 pm
by SidewinderX
I have a birthday in the form yyyy-mm-dd how can I convert that to an age?

Say I have a string

Code: Select all

$dob = "1988-10-10";
How can I calculate the age from that?


Thanks.

Posted: Mon Apr 02, 2007 9:13 pm
by Benjamin
I wrote this a long time ago, the code is kind of nasty but it does the job..

viewtopic.php?t=50354

Posted: Tue Apr 03, 2007 5:07 am
by mikeeeeeeey
Look at it logically, you need to take the current date and assess it from the string $dob

A really simple way, which would need working on would be to take sub strings from a string, and then do a bit of maths, so:

Code: Select all

$year = (int)substr($dob,0,4); //takes the year out

$thisYear = date(Y);

$age = $thisYear - $year;
Then you could build the months into this to be completely sure.

Hope that helps!

Posted: Tue Apr 03, 2007 6:25 am
by stereofrog
If the data is in a mysql table, you can calculate age in your sql query:

Code: Select all

SELECT ....
	YEAR(NOW()) - 
		YEAR(dob) - 	
		(DATE_FORMAT(NOW(), '%m%d') < DATE_FORMAT(dob, '%m%d')) 
	AS age
FROM...