• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php实现根据身份证获取精准年龄

php 搞代码 4年前 (2022-01-01) 22次浏览 已收录 0个评论

这篇文章主要为大家详细介绍了php实现根据身份证获取精准年龄,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

前言

有时候,我们希望通过身份证来计算出年龄,那么下面我写的函数很适合。

实现

代码中已有详细注释。

 function getAge($id){ # 1.从身份证中获取出生日期 $id = $id;//身份证 $birth_Date = strtotime(substr($id, 6, 8));//截取日期并转为时间戳 # 2.格式化[出生日期] $Year = date('Y', $birth_<strong style="color:transparent">来源gaodaima#com搞(代@码网</strong>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

小编再为大家分享一段代码:身份证获取年龄信息:

 /* * 根据身份证号码获取年龄 * inupt $code = 完整的身份证号 * return $age : 年龄 */ function ageVerification($code){ $age_time = strtotime(substr($code, 6, 8)); if($age_time === false){ return false; } list($y1,$m1,$d1) = explode("-",date("Y-m-d",$age_time)); $now_time = strtotime("now"); list($y2,$m2,$d2) = explode("-",date("Y-m-d",$now_time)); $age = $y2 - $y1; if((int)($m2.$d2) <(int)($m1.$d1)){ $age -= 1; } return $age; }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是php实现根据身份证获取精准年龄的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php实现根据身份证获取精准年龄

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址