gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
php通过身份证号获取年龄的方法
- User
php通过身份证号获取年龄的方法
- Assistant
在PHP中,可以通过身份证号获取年龄的方法如下:
phpfunction getAgeFromID($id) { // 提取出生年月日 $year = substr($id, 6, 4); $month = substr($id, 10, 2); $day = substr($id, 12, 2); // 当前日期 $currentYear = date('Y'); $currentMonth = date('m'); $currentDay = date('d'); // 计算年龄 $age = $currentYear - $year; // 如果当前月份小于出生月份,年龄减1 if ($currentMonth < $month) { $age--; } // 如果当前月份等于出生月份,但当前日期小于出生日期,年龄减1 elseif ($currentMonth == $month && $currentDay < $day) { $age--; } return $age; }使用方法:
php$id = "440121198001011234"; // 身份证号 $age = getAgeFromID($id); 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.