Wednesday, August 8, 2012

Is there any PHP function to convert current age to date?


before or on today - 18 years
after today - 19 years

before or on today - 50 years
after today - 51 years

dob <= (today -18 years) and dob > (today - 51 years)

$lower = date('Y-m-d', strtotime('today -18 years'));
$upper = date('Y-m-d', strtotime('today -51 years'));
$query = "SELECT FirstName FROM users WHERE dob >= '$lower' AND dob < '$upper';";


Note that if you plan on dates before 1970 (52-ish), you should likely use DateTime instead of date and sttrtotime (I actually only use DateTime in actual code, but date/strtotime make for much briefer examples).

An example of 85 years ago with DateTime:


$d = new DateTime('today -85 years');
$s = $d->format('Y-m-d'); //1927-06-03 as of 3 June 2012

No comments:

Post a Comment