php根据身份证号码计算年龄的实例代码

<?php
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;
    }
?>php

相关文章
相关标签/搜索