前言
有时候,我们希望通过身份证来计算出年龄,那么下面我写的函数很适合。
实现
代码中已有详细注释。
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
小编再为大家分享一段代码:身份证获取年龄信息:
/* * 根据身份证号码获取年龄 * 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; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月27日
2024年09月27日
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】
- 李恕权.2003-回·20年全精选2CD【SONY】【WAV+CUE】
- 赞多《序章》[FLAC/分轨][129.13MB]
- 群星《完美的她 影视原声带》[320K/MP3][51.98MB]