首页 > 技术文章 > PHP通过身份证号码获取性别、出生日期、年龄等信息

kingboy-xin 2018-11-05 18:06 原文


$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;             //取出年龄值;

推荐阅读