php.根据身份证号判断年龄 php

$id_card= "371300199610033228"; 

//粗体处是出生年月
$age = date('Y') - substr($id_card, 6, 4) + (date('md') >= substr($id_card, 10, 4) ? 1 : 0); 

function getAgeByID($id){ 
        
//过了这年的生日才算多了1周岁 
        if(empty($id)) return ''; 
        $date=strtotime(substr($id,6,8));
//获得出生年月日的时间戳 
        $today=strtotime('today');
//获得今日的时间戳 
        $diff=floor(($today-$date)/86400/365);
//得到两个日期相差的大体年数 
        
//strtotime加上这个年数后得到那日的时间戳后与今日的时间戳相比 
        $age=strtotime(substr($id,6,8).' +'.$diff.'years')>$today?($diff+1):$diff; 
  
        return $age; 
    } 
?>

猜你喜欢

转载自blog.csdn.net/qq_34285103/article/details/91810232