gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
php通过身份证号获取年龄的方法
- User
php通过身份证号获取年龄的方法
- Assistant
要通过身份证号获取年龄,首先需要解析身份证号中的出生日期,然后计算当前年份与出生年份的差值。
下面是一个用 PHP 实现的方法:
function getAgeFromIdCard($idCard) { // 提取出生日期部分 $birthday = substr($idCard, 6, 8); // 按照年、月、日的顺序解析出生日期 $birthYear = substr($birthday, 0, 4); $birthMonth = substr($birthday, 4, 2); $birthDay = substr($birthday, 6, 2); // 获取当前年份、月份、日期 $currentYear = date('Y'); $currentMonth = date('m'); $currentDay = date('d'); // 计算年龄 $age = $currentYear - $birthYear; // 如果当前月份小于出生月份,年龄减一 if ($currentMonth < $birthMonth) { $age--; } // 如果当前月份等于出生月份,而且当前日期小于出生日期,年龄减一 elseif ($currentMonth == $birthMonth && $currentDay < $birthDay) { $age--; } return $age; } // 示例用法 $idCard = '身份证号码'; $age = getAgeFromIdCard($idCard); echo "年龄: " . $age;请将
'身份证号码'替换为要计算年龄的身份证号码。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.