php实现根据身份证获取年龄-php教程

资源魔 25 0

实例代码以下:

(相干视频教程保举:php视频教程)

function getAge($id){

# 1.从身份证中猎取出身日期
$id = $id;//身份证
$birth_Date = strtotime(substr($id, 6, 8));//截取日期并转为工夫戳

# 2.格局化[出身日期]
$Year = date('Y', $birth_Date);//yyyy
$Month = date('m', $birth_Date);//妹妹
$Day = date('d', $birth_Date);//dd

# 3.格局化[以后日期]
$current_Y = date('Y');//yyyy
$current_M = date('m');//妹妹
$current_D = date('d');//dd

# 4.较量争论春秋()
$age = $current_Y - $Year;//往年减去诞辰年
if($Month > $current_M || $Month == $current_M && $Day > $current_D){//深层判别(日)
    $age--;//假如出身月年夜于以后月或出身月等于以后月但出身日年夜于以后日则减一岁
}
# 前往
return $age;

}

应用:

经过挪用 getAge() 办法,传入身份证号便可较量争论。

# 参数必需为 String 型
echo getAge('130322xxxxxxxxxx14');
// xx

保举教程:php教程

以上就是php完成依据身份证猎取春秋的具体内容,更多请存眷资源魔其它相干文章!

标签: php php开发教程 php开发资料 php开发自学

抱歉,评论功能暂时关闭!