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


// 方法二:通过截取年、月、日来进行比较计算,更简单
function getAgeByIdcard($idcard){
$year = substr($idcard, 6, 4);
$monthDay = substr($idcard, 10, 4);

$age = date('Y') - $year;
if ($monthDay > date('md')) {
$age--;
}
return $age;
}

 

 

各位大侠们 看完扫个红包呗,可以省钱,免费的哦,扫完了别忘了用。也可以直接把红包发给我,多少都不闲,就是喜欢听那个声音。!!!!

   

posted @ 2021-09-23 14:33  俊豪(Alvin)  阅读(613)  评论(0编辑  收藏  举报