php 身份证格式校验,年龄计算
生活随笔
收集整理的這篇文章主要介紹了
php 身份证格式校验,年龄计算
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<?php/*** 身份證校驗* @param unknown $id* @return boolean*/public static function isIdcard($id){$id = strtoupper($id);// 檢查18位$regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";@preg_match($regx, $id, $arr_split);$dtm_birth = $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];//檢查生日日期是否正確$date = strtotime($dtm_birth);if(!$date){return false;}// 未滿十八周歲不可注冊$age = self::getAgeByBirth($date,2);if($age < 18){exit(json_encode(array('code'=>0,'msg'=>'未滿18周歲不可注冊!')));}//檢驗18位身份證的校驗碼是否正確。//校驗位按照ISO 7064:1983.MOD 11-2的規定生成,X可以認為是數字10。$arr_int = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];$arr_ch = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];$sign = 0;for ( $i = 0; $i < 17; $i++ ){$b = (int) $id[$i];$w = $arr_int[$i];$sign += $b * $w;}$n = $sign % 11;$val_num = $arr_ch[$n];if ($val_num != substr($id,17, 1)){return false;}else{return true;}}/*** $date是時間戳* $type為1的時候是虛歲,2的時候是周歲*/public static function getAgeByBirth($date,$type = 1){$nowYear = date("Y",time());$nowMonth = date("m",time());$nowDay = date("d",time());$birthYear = date("Y",$date);$birthMonth = date("m",$date);$birthDay = date("d",$date);if($type == 1){$age = $nowYear - ($birthYear - 1);}else{if($nowMonth<$birthMonth){$age = $nowYear - $birthYear - 1;}elseif($nowMonth==$birthMonth){if($nowDay<$birthDay){$age = $nowYear - $birthYear - 1;}else{$age = $nowYear - $birthYear;}}else{$age = $nowYear - $birthYear;}}return $age;}/** 根據身份證號,獲取性別* string $idcard 完整的身份證號* 0未知* 1男* 2女*/public static function getSexById($idcard){// 獲取身份證倒數第二位數字$number = substr($idcard, strlen($idcard) - 2, 1);if ($number % 2 == 0) { // 偶數女return 2;} else { // 基數男return 1;}}
總結
以上是生活随笔為你收集整理的php 身份证格式校验,年龄计算的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: H5思维导图
- 下一篇: 不同分辨率图片匹配_杜克大学开源 AI