欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

根据身*份*证信息获取年龄

程序员文章站 2022-03-01 18:42:26
...
function identityToAge($id){
   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;
}