2015-10-12 147 views
2

我希望能够自己的年龄后,搜索用户:年龄后搜索用户。如何计算正确的年龄?

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

如果我选择年龄:20-50我想20-50岁之间的所有用户旧上市。 如果我选择Age:x-30,我希望所有30岁以上的用户都可以上市。

在分贝我用这种格式保存用户的生日:2013-03-03。

要计算用户的年龄和echo开我做这个网页的年龄:

<?php 

$ag=$row['age']; 
$diff=time()-strtotime($ag); 
$age=date('Y-m-d', strtotime($ag)); 

echo floor($diff/60/60/24/365.25); 
?> 

如何使一个选择查询,其中年龄> $ A1和< $ A2?

回答

1

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

$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"; 
} 

验证和查询抛光留作练习计算日期。

+0

我得到解析错误:语法错误,意外的'$ query'在 $ query。=“AND”; 和 $ query。=“'$ age'<$ maxdate”; – anon

+0

$ 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 = mysqli_query($ con,“SELECT * FROM users WHERE c ='$ c'”); $ row = mysqli_fetch_array($ query); $ age = $ row ['age']; if(!is_null($ mindate)) $ query。=“'$ age'>'$ mindate'”; 如果{ 如果(is_null($ MINDATE) $查询= “和”!。 $查询= “ '$时代'<$的maxDate”。 }(is_null($的maxDate)!) – anon

+0

@anon我定义$查询为字符串,您将其定义为mysqly_query的结果... – Ilario