$sex = substr($idcard, (strlen($idcard)==18 ? -2 : -1), 1) % 2 ? '1' : '2'; //18位身份证取性别,倒数第二位奇数是男,偶数是女;
$birthday = strlen($idcard)==15 ? ('19' . substr($idcard, 6, 6)) : substr($idcard, 6, 8); //取身份证年月日;
$birthdays = strtotime(strlen($idcard)==15 ? ('19' . substr($idcard, 6, 6)) : substr($idcard, 6, 8)); //身份证年月日转换成时间戳
$today = strtotime('today'); //取当天日期;
$diff = floor(($today-$birthdays)/86400/365); //用时间戳相减算出年龄;
$age = strtotime(substr($idcard,6,8).'+'.$diff.'years')>$today?($diff+1):$diff; //取出年龄值;
转载于:https://www.cnblogs.com/kingboy-xin/p/9910552.html