Calculate Age: One-Liner Fun
September 20, 2008 1:06 pm PHPHere's a little one-liner I thought up today for this PHPBuilder forum thread. It's purpose is to calculate someone's age when you know the year, month, and day of their birth (integer values). In this snippet it is assumed that $year, $month, and $day hold the integer values for the birthday of interest.
$today = time();
for($yr = $year, $age = -1; mktime(0,0,0,$month,$day,$yr) < $today; $yr++, $age++);
echo $age;
This utilizes the often ignored fact that you can use comma-separated statements for the first and third expressions in the for loop definition list.

nrg_alpha :
Date: September 25, 2008 @ 22:47
Nice little script. Does indeed remind the rest of us that you can have multiple first and third comma-separated statements. I never made use of this, but can see where it streamlines things.