php实现根据输入的年龄查询出生日期符合的数据

需求描述: 前端发送请求如33-55岁,后端拿到年龄数据后将指定年龄转为日期

$search = $_POST;
$year = date('Y');
$month = date('m');
$day = date('d');
$max_birth = '';
$min_birth = '';
// 用户输入了最小年龄和最大年龄
if(isset($search['person_age_min']) && isset($search['person_age_max'])){
    $min = min($search['person_age_min'], $search['person_age_max']);
    $max = max($search['person_age_min'], $search['person_age_max']);
    # 计算最大年龄的最早出生日期
    $max_year = $year - $max - 1;
    $max_birth = date('Y-m-d', strtotime("+1 day", strtotime($max_year.'-'.$month.'-'.$day)));
    # 计算最小年龄的最后出生日期
    $min_year = $year-$min;
    $min_birth = $min_year.'-'.$month.'-'.$day;
}elseif(isset($search['person_age_min'])  && !isset($search['person_age_max'])){ //用户只输入了最小年龄
    # 计算最大年龄的最早出生日期
    $max_year = $year - $search['person_age_min'] - 1;
    $max_birth = date('Y-m-d', strtotime("+1 day", strtotime($max_year.'-'.$month.'-'.$day)));
    # 计算最小年龄的最后出生日期
    $min_year = $year-$search['person_age_min'];
    $min_birth = $min_year.'-'.$month.'-'.$day;
}elseif(!isset($search['person_age_min'])  && isset($search['person_age_max'])) { // 用户只输入了最大年龄
    # 计算最大年龄的最早出生日期
    $max_year = $year - $search['person_age_max'] - 1;
    $max_birth = date('Y-m-d', strtotime("+1 day", strtotime($max_year.'-'.$month.'-'.$day)));
    # 计算最小年龄的最后出生日期
    $min_year = $year-$search['person_age_min'];
    $min_birth = $min_year.'-'.$month.'-'.$day;
}
if(!empty($min_birth) && !empty($max_birth)){
    // sql语句
    $sql = "select * from user where birth between $max_birth and $min_birth";
    
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值