//根據身份證號提取生日日期
function getBirthdayByIdCode($idCode)
{
$idCode = trim($idCode);
if (empty($idCode) || (strlen($idCode) != 18 && strlen($idCode) != 15)) {
return '';
}
$birth = '';
if (strlen($idCode) == 18) {
$birth = substr($idCode, 6, 8);
} else if (strlen($idCode) == 15) {
$birth = '19' . substr($idCode, 6, 6);
}
return substr($birth, 0, 4) . '-' . substr($birth, 4, 2) . '-' . substr($birth, 6, 2);
}
//根據身份證號碼提取性別
function getSexByIdCode($idCode)
{
$idCode = trim($idCode);
$sex = 0;
if (strlen($idCode) == 18) {
$sex = intval(substr($idCode, 16, 1));
} else if (strlen($idCode) == 15) {
$sex = intval(substr($idCode, 14, 1));
}
$rtn = $sex > 0 ? ($sex % 2 == 0 ? 2 : 1) : 0;
if ($rtn == 1) {
return '男';
} elseif ($rtn == 2) {
return '女';
} else {
return '未知';
}
}
$idCode = '110000199306163123';
$birthday = getBirthdayByIdCode($idCode);
$sex = getSexByIdCode($idCode);
var_dump($birthday,$sex);