Home > php教程 > php手册 > body text

php根据身份证号码计算年龄的实例代码

WBOY
Release: 2016-06-06 20:25:30
Original
1469 people have browsed it

我们只要知道身份证的生成规则就可以了,像下面我们从指定位置到多少位就是出日期了,然后我们把日期转成时间戳然后进行加减运算就得出了年龄了,下面我们看实例

复制代码 代码如下:


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

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!