/**
* 验证身份证
* @param $id_card
* @return bool
*/
function check_id_card($id_card){
if(strlen($id_card) != 18){
return false;
}
$id_card_base = substr($id_card,0,17);
if(id_card_verify_number($id_card_base) != strtoupper(substr($id_card,17,1))){
return false;
}else{
return true;
}
}
/**
* 计算身份证校验码,根据国家标准GB 11643-1999
* @param $id_card_base
* @return bool|mixed
*/
function id_card_verify_number($id_card_base){
if(strlen($id_card_base)!=17){
return false;
}
//加权因子
$factor = array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2);
//校验码对应值
$verify_number_list = array('1','0','X','9','8','7','6','5','4','3','2');
$checksum = 0;
for($i=0;$i<strlen($id_card_base);$i++){
$checksum += substr($id_card_base,$i,1) * $factor[$i];
}
$mod = $checksum % 11;
$verify_number = $verify_number_list[$mod];
return $verify_number;
}
/**
* 根据身份证号码获取性别
* @param $id_card
* @return string|null
*/
function get_sex($id_card) {
if(empty($id_card)) return null;
$sexInt = substr($id_card, 16, 1);
return $sexInt % 2 === 0 ? '女' : '男';
}
/**
* 获取出生年月
* @param string $id_card 操作数据
* @return mixed
*/
function get_birthday($id_card){
$year = substr($id_card, 6, 4);
$month = substr($id_card, 10, 2);
$day = substr($id_card, 12, 2);
return $year.'-'.$month.'-'.$day;
}
/**
* 获取周岁
* @param string $id_card 操作数据
* @return mixed
*/
function get_age($id_card){
$now_year = date('Y');
$now_month = date('m');
$now_day = date('d');
$year = substr($id_card, 6, 4);
$month = substr($id_card, 10, 2);
$day = substr($id_card, 12, 2);
$age = $now_year - $year - 1;
if ($month < $now_month || ($month == $now_month && $day <= $now_day)){
$age++;
}
return $age;
}
|