PHP通过身份证号码获取性别、出生日期、年龄等信息

博客介绍了常用的PHP方法,可利用身份证号码计算年龄、时间戳等。包含从18位身份证取性别的方法,取身份证年月日并转换为时间戳,通过时间戳相减算出年龄,还给出了取出年龄值的具体代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

常用的php用身份证号码计算年龄、计算时间戳等方法!
//18位身份证取性别,倒数第二位奇数是男,偶数是女;
sex=substr(sex = substr(sex=substr(idcard, (strlen($idcard)==18 ? -2 : -1), 1) % 2 ? ‘1’ : ‘2’;
//取身份证年月日;
birthday=strlen(birthday = strlen(birthday=strlen(idcard)==15 ? (‘19’ . substr(idcard,6,6)):substr(idcard, 6, 6)) : substr(idcard,6,6)):substr(idcard, 6, 8);
//身份证年月日转换成时间戳
birthdays=strtotime(strlen(birthdays = strtotime(strlen(birthdays=strtotime(strlen(idcard)==15 ? (‘19’ . substr(idcard,6,6)):substr(idcard, 6, 6)) : substr(idcard,6,6)):substr(idcard, 6, 8));
//取当天日期;
$today = strtotime(‘today’);
//用时间戳相减算出年龄;                            
diff=floor((diff = floor((diff=floor((today-$birthdays)/86400/365);    
//取出年龄值;                        
age=strtotime(substr(age = strtotime(substr(age=strtotime(substr(idcard,6,8).’+’.diff.′years′)>diff.'years')>diff.years)>today?(diff+1):diff+1):diff+1):diff;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值