|
|
|
|
@ -5,25 +5,6 @@ use DateTime;
|
|
|
|
|
class Tools{
|
|
|
|
|
//根据当前日期和身份证获取年龄
|
|
|
|
|
public static function calculateAgeFromID($idNumber, $targetDate) {
|
|
|
|
|
// // 提取出生年月日
|
|
|
|
|
// $birthYear = substr($idNumber, 6, 4);
|
|
|
|
|
// $birthMonth = substr($idNumber, 10, 2);
|
|
|
|
|
// $birthDay = substr($idNumber, 12, 2);
|
|
|
|
|
//
|
|
|
|
|
// // 创建出生日期和目标日期的 DateTime 对象
|
|
|
|
|
// $birthdate = new DateTime("$birthYear-$birthMonth-$birthDay");
|
|
|
|
|
// $target = new DateTime($targetDate);
|
|
|
|
|
//
|
|
|
|
|
// // 计算年龄
|
|
|
|
|
// $interval = $birthdate->diff($target);
|
|
|
|
|
// $age = $interval->y;
|
|
|
|
|
//
|
|
|
|
|
// // 如果生日还没到,减一岁
|
|
|
|
|
// if ($target->format('m-d') < $birthdate->format('m-d')) {
|
|
|
|
|
// $age--;
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// return $age;
|
|
|
|
|
// 提取出生年月日
|
|
|
|
|
$birthYear = substr($idNumber, 6, 4);
|
|
|
|
|
$birthMonth = substr($idNumber, 10, 2);
|
|
|
|
|
@ -64,4 +45,21 @@ class Tools{
|
|
|
|
|
// 格式化并返回日期字符串
|
|
|
|
|
return date('Y-m-d', strtotime($birthday));
|
|
|
|
|
}
|
|
|
|
|
//根据身份证判断性别
|
|
|
|
|
public static function getGenderFromIDCard($idCard) {
|
|
|
|
|
// 检查身份证号是否合法(长度应该是18)
|
|
|
|
|
if (strlen($idCard) != 18) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取身份证号的第17位
|
|
|
|
|
$genderBit = intval($idCard[16]);
|
|
|
|
|
|
|
|
|
|
// 判断性别
|
|
|
|
|
if ($genderBit % 2 == 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|