php如何算出身份证上面的年龄

gz109 2017-02-18 03:46:38
比如从数据库里面提取到了身份证号码,,431381197408191515
然后我用substr截取到出生日期,我想算出这个身份证 号码当前多少岁
思路就是当前时间戳,减去身份证日期,得出时间戳,然后再换算成年


$no='431381197408191515';
$one=substr($no,6,8); //提权中间时间
$y=substr($one,0,4); //提权年
$m=substr($one,4,2); //提取月
$d=substr($one,6,2); //提取天
$all=$y.$m.$d; //完整的生日日期
//拿到的值是 1974-08-19


//然后把拿到的日期格式转换成时间戳
$two=strtotime($all); //但是这里又有问题哦,因为我后面没有具体的小时分钟
//然后就是当前日期减去拿到的时间戳,然后再换算
$alltime=time()-$two;

//下面的东西我就不会写了,




...全文
546 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
similing 2017-02-21
  • 打赏
  • 举报
回复
不用时间戳做差,拿身份证的生日和time()比,先比月日谁更晚(先比月后比日),然后年作差,刚才比较的月日身份证的晚就减一岁,否则就不变
voidbit 2017-02-21
  • 打赏
  • 举报
回复

$no = '431381197408191515';
$year = substr($no, 6, 4);
$monthDay = substr($no, 10, 4);
 
$age = date('Y') - $year;
if ($monthDay > date('md')) {
    $age--;
}
 
echo $age;
voidbit 2017-02-21
  • 打赏
  • 举报
回复

$no = '431381197408191515';
$year = substr($no, 6, 4);
$month = substr($no, 10, 2);
$day = substr($no, 12, 2);

$age = date('Y') - $year;
if ($month > date('m') || $day > date('d')) {
    $age--;
}

echo $age;
傲雪星枫 2017-02-18
  • 打赏
  • 举报
回复

$no='431381197408191515';
$one=substr($no,6,8);    //提权中间时间
$y=substr($one,0,4);    //提权年
$m=substr($one,4,2);    //提取月
$d=substr($one,6,2);    //提取天

$birth = $y.'-'.$m.'-'.$d;
echo getAge($birth);

/**
 * 獲取年齡
 * @param date $birth 日期
 * @return int $age
 */
function getAge($birth) {
    $mybirth = getdate(strtotime($birth));
    $year = $mybirth['year'];
    $month = $mybirth['mon'];
    $day = $mybirth['mday'];

    $ndate = getdate();
    $nyear = $ndate['year'];
    $nmonth = $ndate['mon'];
    $nday = $ndate['mday'];

    $age = $nyear - $year;

    if (($nmonth * 100 + $nday) - ($month * 100 + $day) < 0) {
        $age = $age - 1;
    }
    return $age;
} 
42
xuzuning 2017-02-18
  • 打赏
  • 举报
回复
$no = '431381197408191515';
echo date('Y') - substr($no, 6, 4);

//足岁
echo date('Y') - substr($no, 6, 4) + (date('md') >= substr($no, 10, 4) ? 1 : 0);
DavidNineRoc 2017-02-18
  • 打赏
  • 举报
回复
date("Y-m-d");得到当前年月日 explode("-");分割当前的年月日 然后和你得到的分组计算就行了

20,359

社区成员

发帖
与我相关
我的任务
社区描述
“超文本预处理器”,是在服务器端执行的脚本语言,尤其适用于Web开发并可嵌入HTML中。PHP语法利用了C、Java和Perl,该语言的主要目标是允许web开发人员快速编写动态网页。
phpphpstorm 技术论坛(原bbs)
社区管理员
  • 开源资源社区
  • phpstory
  • xuzuning
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧