当前位置:首页 > Web开发 > 正文

我们希望通过身份证来计算出年龄

2024-03-31 Web开发

有时候,qq空间权限破解 ,我们但愿通过身份证来计算出春秋,那么下面我写的函数很适合。

实现
代码中已有详细注释。

function getAge($id){

# 1.从身份证中获取出生日期
$id = $id;//身份证
$birth_Date = strtotime(substr($id, 6, 8));//截取日期外汇返佣并转为时间戳

# 2.格局化[出生日期]
$Year = date(‘Y‘, $birth_Date);//yyyy
$Month = date(‘m‘, $birth_Date);//mm
$Day = date(‘d‘, $birth_Date);//dd

# 3.格局化[当前日期]
$current_Y = date(‘Y‘);//yyyy
$current_M = date(‘m‘);//mm
$current_D = date(‘d‘);//dd

# 4.计算春秋()
$age = $current_Y - $Year;//本年减去生日年
if($Month > $current_M || $Month == $current_M && $Day > $current_D){//深层判断(日)
$age--;//如果出生月大于当前月或出生月即是当前月但出生日大于当前日则减一岁
}
# 返回
return $age;

}

使用

通过挪用 getAge() 要领,传入身份证号即可计算。

# 参数必需为 String 型 echo getAge(‘130322xxxxxxxxxx14‘); // xx

php 实现按照身份证获取精准春秋

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/29339.html