首页 > 编程 > PHP > 正文

php根据身份证号码计算年龄

2019-11-15 01:21:40
字体:
来源:转载
供稿:网友
php根据身份证号码计算年龄

身份证的生成规则,从指定位置到多少位就是出日期了,然后把日期转成时间戳然后进行加减运算就得出年龄.

代码:

<?php/*** 根据身份证号码得到年龄* edit: www.jbxue.com*/function getAgeByID($id){//过了这年的生日才算多了1周岁 if(empty($id)) return ''; $date=strtotime(substr($id,6,8));//获得出生年月日的时间戳 $today=strtotime('today');//获得今日的时间戳 $diff=floor(($today-$date)/86400/365);//得到两个日期相差的大体年数 //strtotime加上这个年数后得到那日的时间戳后与今日的时间戳相比 $age=strtotime(substr($id,6,8).' +'.$diff.'years')>$today?($diff+1):$diff;return $age; } ?>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表