php实现根据身份证获取年龄
来源:V
发布时间:2020-05-14 09:48:22
阅读量:2889

实例代码如下:
(相关视频教程推荐:php视频教程)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | function getAge($id){
# 1.从身份证中获取出生日期
$id = $id;
$birth_Date = strtotime(substr($id, 6, 8));
# 2.格式化[出生日期]
$Year = date('Y', $birth_Date);
$Month = date('m', $birth_Date);
$Day = date('d', $birth_Date);
# 3.格式化[当前日期]
$current_Y = date('Y');
$current_M = date('m');
$current_D = date('d');
# 4.计算年龄()
$age = $current_Y - $Year;
if($Month > $current_M || $Month == $current_M && $Day > $current_D){
$age--;
}
# 返回
return $age;
}
|
使用:
通过调用 getAge() 方法,传入身份证号即可计算。
1 2 3 | # 参数必须为 String 型
echo getAge('130322xxxxxxxxxx14');
|