php 获取身份证号信息 及 计算周岁年龄

php 获取身份证号信息 及 计算周岁年龄


//获取身份证号信息
function idnumber_info($idnumber){
$info = [];
//户口所在地
$area_code = substr($idnumber, 0, 4) ;
$info["birthplace"] = $area_code;

//生日
$birthday = substr($idnumber, 6, 4)
. "-" . substr($idnumber,10, 2)
. "-" . substr($idnumber,12, 2);
$info["birthday"] = $birthday;
//性别
if (intval(substr($idnumber,16, 1)) % 2 == 1) {
//男
$sex = "男";
} else {
//女
$sex = "女";
}
$info["sex"] = $sex;
//年龄
$info["age"] = compute_age($birthday);
return $info;
}

//计算年龄方法
function compute_age($birthday){
if($birthday) {
$time = strtotime($birthday);
$year = date("Y",$time);
$age = date("Y") - $year -1;
$month = date("m",$time);
$day = date("d",$time);

$c_m = date("m");
$c_d = date("d");

if ( $month < $c_m || $month == $c_m && $day <= $c_d) {
$age++;
}
}else{
$age = 0;
}

return $age;
}


Posted in php