按年龄搜索用户.如何计算出正确的确定年龄


Search user after age. How to calculate the correct determined age?

我希望能够在用户年龄之后搜索用户:

(Age input saved in variables $a1 - $a2)
Age: □ - □ 
     -Search button-

如果我选择年龄:20-50,我希望列出所有年龄在20-50岁之间的用户。如果我选择年龄:x-30,我希望列出所有年龄不超过30岁的用户。

在数据库中,我用以下格式保存用户的生日:2013-03-03.

要计算用户的年龄并在网页上显示年龄,我这样做:

<?php 
$ag=$row['age'];
$diff=time()-strtotime($ag);
$age=date('Y-m-d', strtotime($ag));
echo floor($diff/60/60/24/365.25); 
?>

如何创建一个Select查询where age> $a1 and <a2美元?>

不是从返回的日期计算年龄,而是从期望的年龄计算日期

$mindate = is_numeric($a2)? date("Y-m-d", strtotime('-$a2 year')) : null;
$maxdate = is_numeric($a1)? date("Y-m-d", strtotime('-$a1 year')) : null;
$query = "SELECT * FROM whatevertable WHERE ";
if(!is_null($mindate))
   $query .= "datecolumn > '$mindate'";
if(!is_null($maxdate)){
   if(!is_null($mindate)
      $query .= " AND ";
   $query .= "datecolumn < $maxdate";
}

验证和查询抛光作为练习。