How to Calculate Age with PHP

Found this cool snippet with returns how old is somebody from a given birthday.

function birthday($birthday){

list($year,$month,$day) = explode("-",$birthday);
$year_diff = date("Y") - $year;
$month_diff = date("m") - $month;
$day_diff = date("d") - $day;
if ($day_diff < 0 || $month_diff < 0) $year_diff--; return $year_diff; }

Please note there is a lot of variatons and I'm sure it could be done better.

About Rick

Senior Front-end Software Engineer from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.
8 comments

Leave a Reply

Add <code> Some Code </code> by using this tags.

*
*