我們只要知道身份證的生成規則就可以了,像下面我們從指定位置到多少位就是出日期了,然后我們把日期轉成時間戳然后進行加減運算就得出了年齡了,下面我們看實例代碼如下:
- <?php
- function getAgeByID($id){
- //過了這年的生日才算多了1周歲
- if(emptyempty($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;
- }
- ?>
新聞熱點
疑難解答