Converting age to date of birth

I’m building a form that filters through users based on the selected age range. My database stores users’ birthday in the form year-month-day (ex. 1986-04-23). I’m having trouble writing a function that takes the age subtracts the years from today’s date and converts it back to a date that I can use to compare with those in my database.

    
   function reverse_birthday($age) {
	list($year,$month,$day) = explode("-", date("Y-m-d"));
	$range = $year - $age;
	return strtotime("{$range}-{$month}-{$day}");
   }
  

So for example if I pass the function “18” it should return “1993-04-15” based on today’s date.

I’m trying to use the strtotime function to accomplish this but can’t seem to get it to work properly. I’ve checked out the PHP manual and read about this function with no avail.

Any suggestions would be appreciated. thanks.

Why not convert to a date first and then subtract the number of years. Just subtracting the age from the year portion could result in an invalid date where the 29th February is involved.

I would think that if you are trying to find user(s) w/in a certain age range, that you would use a todays date type function and subtract that from user inputed information to obtain your range of user(s).

Unbelievably simple really.

function reverse_birthday( $years ){
return date('Y-m-d', strtotime($years . ' years ago'));
}

$bd = reverse_birthday(18);
echo $bd; // 1993-04-16

Thanks for all the input I didn’t realize how just the date function alone could make it so simple.

I think you are doing it wrong. You should just ask user to enter date of birth instead of their age. You can always calculate age based on date of birth but you can’t get date of birth based on age.

Here is a simple function to get age based on date of birth:

$dob = ‘1990/04/21’;
$oDOB = new DateTime($dob);
$oDiff = $oDOB->diff(new DateTime(), true);

echo $oDiff->format(“%y”);

Users already entered their date of birth when they registered and their DOB is stored in the database. What I was trying to accomplish was a search filter where a user can find other users within a certain age range. The filter would take that age range and search the database for users with a DOB that matches the specified range.

That can be done far more efficiently using SQL in your database query.

Here are the basics*.


SELECT name from users 
WHERE dob < DATE_ADD('1990-01-01', INTERVAL 2 YEAR) 
AND dob > DATE_ADD('1990-01-01', INTERVAL -2 YEAR)

(where ‘dob’ is a date of birth date field)

Just substitute variables for the year length (2) and the target dob (‘1990-01-01’ in this case).

You could just then wrap that in a function:


$youragegroup = getSimilarAgedPeople('1990-01-01', 2);

  • untested, and I am unsure as to whether DATE_ADD is a mysql specific function - there will likely be other ways of doing this, ask on the sql forums on this site.

More real-world example of the above would be:

Find users from [18] to [30]

Generates this:


$age1 = 18;
$age2 = 30+1; // Have to add 1 to the second number or it will end with people who just turned 30!
$sql = "SELECT name 
        FROM users 
        WHERE dob 
            BETWEEN DATE_ADD(CURDATE(), INTERVAL -{$age1} YEAR) 
                AND DATE_ADD(CURDATE(), INTERVAL -{$age2} YEAR)";