当前位置:Gxlcms > PHP教程 > PHP的身份证号码工具类翻译的那个Java的

PHP的身份证号码工具类翻译的那个Java的

时间:2021-07-01 10:21:17 帮助过:31人阅读

实现从15位~18位的身份证号码转换,校验中国大陆居民身份证、香港居民身份证、澳门身份证和台湾身份证。
  1. /**
  2. * 身份证工具类
  3. *
  4. * @author Tongle Xu 2013-6-6
  5. * @copyright Copyright (c) 2003-2103 tintsoft.com
  6. * @license http://www.tintsoft.com
  7. * @version $Id$
  8. */
  9. class Utils_Idcard {
  10. /**
  11. * 中国公民身份证号码最小长度。
  12. */
  13. const CHINA_ID_MIN_LENGTH = 15;
  14. /**
  15. * 中国公民身份证号码最大长度。
  16. */
  17. const CHINA_ID_MAX_LENGTH = 18;
  18. /**
  19. * 最低年限
  20. */
  21. const MIN = 1930;
  22. /**
  23. * 省、直辖市代码表
  24. */
  25. public static $cityCode = array ("11","12","13","14","15","21","22","23","31","32","33","34","35","36","37","41","42","43","44","45","46","50","51","52","53","54","61","62","63","64","65","71","81","82","91" );
  26. /**
  27. * 每位加权因子
  28. */
  29. public static $power = array (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 );
  30. /**
  31. * 第18位校检码
  32. */
  33. public static $verifyCode = array ("1","0","X","9","8","7","6","5","4","3","2" );
  34. /**
  35. * 国内身份证校验
  36. */
  37. public static $cityCodes = array ('11' => '北京' );
  38. /**
  39. * 将15位身份证号码转换为18位
  40. *
  41. * @param idCard 15位身份编码
  42. * @return 18位身份编码
  43. */
  44. public static function conver15CardTo18($idCard) {
  45. $idCard18 = "";
  46. if (strlen ( $idCard ) != self::CHINA_ID_MIN_LENGTH) {
  47. return null;
  48. }
  49. if (self::isNum ( $idCard )) {
  50. // 获取出生年月日
  51. $sYear = '19' . substr ( $idCard, 6, 2 );
  52. $idCard18 = substr ( $idCard, 0, 6 ) . $sYear . substr ( $idCard, 8 );
  53. // 转换字符数组
  54. $iArr = str_split ( $idCard18 );
  55. if ($iArr != null) {
  56. $iSum17 = self::getPowerSum ( $iArr );
  57. // 获取校验位
  58. $sVal = self::getCheckCode18 ( $iSum17 );
  59. if (strlen ( $sVal ) > 0) {
  60. $idCard18 .= $sVal;
  61. } else {
  62. return null;
  63. }
  64. }
  65. } else {
  66. return null;
  67. }
  68. return $idCard18;
  69. }
  70. /**
  71. * 验证身份证是否合法
  72. */
  73. public static function validateCard($idCard) {
  74. $card = trim ( $idCard );
  75. if (self::validateIdCard18 ( $card )) {
  76. return true;
  77. }
  78. if (self::validateIdCard15 ( $card )) {
  79. return true;
  80. }
  81. return false;
  82. }
  83. /**
  84. * 验证18位身份编码是否合法
  85. *
  86. * @param int $idCard 身份编码
  87. * @return boolean 是否合法
  88. */
  89. public static function validateIdCard18($idCard) {
  90. $bTrue = false;
  91. if (strlen ( $idCard ) == self::CHINA_ID_MAX_LENGTH) {
  92. // 前17位
  93. $code17 = substr ( $idCard, 0, 17 );
  94. // 第18位
  95. $code18 = substr ( $idCard, 17, 1 );
  96. if (self::isNum ( $code17 )) {
  97. $iArr = str_split ( $code17 );
  98. if ($iArr != null) {
  99. $iSum17 = self::getPowerSum ( $iArr );
  100. // 获取校验位
  101. $val = self::getCheckCode18 ( $iSum17 );
  102. if (strlen ( $val ) > 0 && $val == $code18) {
  103. $bTrue = true;
  104. }
  105. }
  106. }
  107. }
  108. return $bTrue;
  109. }
  110. /**
  111. * 验证15位身份编码是否合法
  112. *
  113. * @param string $idCard 身份编码
  114. * @return boolean 是否合法
  115. */
  116. public static function validateIdCard15($idCard) {
  117. if (strlen ( $idCard ) != self::CHINA_ID_MIN_LENGTH) {
  118. return false;
  119. }
  120. if (self::isNum ( $idCard )) {
  121. $proCode = substr ( $idCard, 0, 2 );
  122. if (! isset ( self::$cityCodes [$proCode] )) {
  123. return false;
  124. }
  125. //升到18位
  126. $idCard = self::conver15CardTo18($idCard);
  127. return self::validateIdCard18($idCard);
  128. } else {
  129. return false;
  130. }
  131. return true;
  132. }
  133. /**
  134. * 根据身份编号获取年龄
  135. *
  136. * @param string idCard 身份编号
  137. * @return 年龄
  138. */
  139. public static function getAgeByIdCard($idCard) {
  140. $iAge = 0;
  141. if (strlen ( $idCard ) == self::CHINA_ID_MIN_LENGTH) {
  142. $idCard = self::conver15CardTo18 ( $idCard );
  143. }
  144. $year = substr ( $idCard, 6, 4 );
  145. $iCurrYear = date ( 'Y', time () );
  146. $iAge = $iCurrYear - $year;
  147. return $iAge;
  148. }
  149. /**
  150. * 根据身份编号获取生日天
  151. *
  152. * @param string $idCard 身份编号
  153. * @return NULL string
  154. */
  155. public static function getDateByIdCard($idCard) {
  156. $len = strlen ( $idCard );
  157. if ($len < self::CHINA_ID_MIN_LENGTH) {
  158. return null;
  159. } else if ($len == CHINA_ID_MIN_LENGTH) {
  160. $idCard = self::conver15CardTo18 ( $idCard );
  161. }
  162. return substr ( $idCard, 12, 2 );
  163. }
  164. /**
  165. * 根据身份编号获取性别
  166. *
  167. * @param string $idCard 身份编号
  168. * @return 性别(M-男,F-女,N-未知)
  169. */
  170. public static function getGenderByIdCard($idCard) {
  171. $sGender = "N";
  172. if (strlen ( $idCard ) == self::CHINA_ID_MIN_LENGTH) {
  173. $idCard = self::conver15CardTo18 ( $idCard );
  174. }
  175. $sCardNum = substr ( $idCard, 16, 1 );
  176. if (( int ) $sCardNum % 2 != 0) {
  177. $sGender = "M";
  178. } else {
  179. $sGender = "F";
  180. }
  181. return $sGender;
  182. }
  183. /**
  184. * 根据身份编号获取户籍省份
  185. *
  186. * @param string $idCard 身份编号
  187. * @return string
  188. */
  189. public static function getProvinceByIdCard($idCard) {
  190. $len = strlen ( $idCard );
  191. $sProvince = null;
  192. $sProvinNum = "";
  193. if ($len == self::CHINA_ID_MIN_LENGTH || $len == self::CHINA_ID_MAX_LENGTH) {
  194. $sProvinNum = substr ( $idCard, 0, 2 );
  195. }
  196. $sProvince = self::$cityCodes [$sProvinNum];
  197. return $sProvince;
  198. }
  199. /**
  200. * 数字验证
  201. *
  202. * @param int $val
  203. */
  204. public static function isNum($val) {
  205. return $val == null || $val == "" ? false : 0 < preg_match ( '/^[0-9]*$/', $val );
  206. }
  207. /**
  208. * 验证小于当前日期 是否有效
  209. *
  210. * @param int $iYear 待验证日期(年)
  211. * @param int $iMonth 待验证日期(月 1-12)
  212. * @param int $iDate 待验证日期(日)
  213. * @return 是否有效
  214. */
  215. public static function valiDate($iYear, $iMonth, $iDate) {
  216. $year = date ( 'Y', time () );
  217. if ($iYear < self::MIN || $iYear >= $year) {
  218. return false;
  219. }
  220. if ($iMonth < 1 || $iMonth > 12) {
  221. return false;
  222. }
  223. switch ($iMonth) {
  224. case 4 :
  225. case 6 :
  226. case 9 :
  227. case 11 :
  228. $datePerMonth = 30;
  229. break;
  230. case 2 :
  231. $dm = (($iYear % 4 == 0 && $iYear % 100 != 0) || ($iYear % 400 == 0)) && ($iYear > self::MIN && $iYear < $year);
  232. $datePerMonth = $dm ? 29 : 28;
  233. break;
  234. default :
  235. $datePerMonth = 31;
  236. }
  237. return ($iDate >= 1) && ($iDate <= $datePerMonth);
  238. }
  239. /**
  240. * 将身份证的每位和对应位的加权因子相乘之后,再得到和值
  241. *
  242. * @param array $iArr
  243. * @return 身份证编码。
  244. */
  245. private static function getPowerSum($iArr) {
  246. $iSum = 0;
  247. $power_len = count ( self::$power );
  248. $iarr_len = count ( $iArr );
  249. if ($power_len == $iarr_len) {
  250. for($i = 0; $i < $iarr_len; $i ++) {
  251. for($j = 0; $j < $power_len; $j ++) {
  252. if ($i == $j) {
  253. $iSum = $iSum + $iArr [$i] * self::$power [$j];
  254. }
  255. }
  256. }
  257. }
  258. return $iSum;
  259. }
  260. /**
  261. * 将power和值与11取模获得余数进行校验码判断
  262. *
  263. * @param int $iSum
  264. * @return 校验位
  265. */
  266. private static function getCheckCode18($iSum) {
  267. $sCode = "";
  268. switch ($iSum % 11) {
  269. case 10 :
  270. $sCode = "2";
  271. break;
  272. case 9 :
  273. $sCode = "3";
  274. break;
  275. case 8 :
  276. $sCode = "4";
  277. break;
  278. case 7 :
  279. $sCode = "5";
  280. break;
  281. case 6 :
  282. $sCode = "6";
  283. break;
  284. case 5 :
  285. $sCode = "7";
  286. break;
  287. case 4 :
  288. $sCode = "8";
  289. break;
  290. case 3 :
  291. $sCode = "9";
  292. break;
  293. case 2 :
  294. $sCode = "x";
  295. break;
  296. case 1 :
  297. $sCode = "0";
  298. break;
  299. case 0 :
  300. $sCode = "1";
  301. break;
  302. }
  303. return $sCode;
  304. }
  305. }
  1. /**
  2. * 身份证工具类
  3. *
  4. * @author Tongle Xu 2013-6-6
  5. * @copyright Copyright (c) 2003-2103 tintsoft.com
  6. * @license http://www.tintsoft.com
  7. * @version $Id$
  8. */
  9. class Utils_Idcard {
  10. /**
  11. * 中国公民身份证号码最小长度。
  12. */
  13. const CHINA_ID_MIN_LENGTH = 15;
  14. /**
  15. * 中国公民身份证号码最大长度。
  16. */
  17. const CHINA_ID_MAX_LENGTH = 18;
  18. /**
  19. * 最低年限
  20. */
  21. const MIN = 1930;
  22. /**
  23. * 省、直辖市代码表
  24. */
  25. public static $cityCode = array ("11","12","13","14","15","21","22","23","31","32","33","34","35","36","37","41","42","43","44","45","46","50","51","52","53","54","61","62","63","64","65","71","81","82","91" );
  26. /**
  27. * 每位加权因子
  28. */
  29. public static $power = array (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 );
  30. /**
  31. * 第18位校检码
  32. */
  33. public static $verifyCode = array ("1","0","X","9","8","7","6","5","4","3","2" );
  34. /**
  35. * 国内身份证校验
  36. */
  37. public static $cityCodes = array ('11' => '北京','12' => '天津','13' => '河北','14' => '山西','15' => '内蒙古','21' => '辽宁','22' => '吉林','23' => '黑龙江','31' => '上海','32' => '江苏','33' => '浙江','34' => '安徽','35' => '福建','36' => '江西','37' => '山东','41' => '河南','42' => '湖北','43' => '湖南','44' => '广东','45' => '广西',
  38. '46' => '海南','50' => '重庆','51' => '四川','52' => '贵州','53' => '云南','54' => '西藏','61' => '陕西','62' => '甘肃','15' => '内蒙古','21' => '辽宁','22' => '吉林','23' => '黑龙江','31' => '上海','32' => '江苏','33' => '浙江','34' => '安徽','35' => '福建','36' => '江西','37' => '山东','41' => '河南','42' => '湖北','43' => '湖南',
  39. '44' => '广东','45' => '广西','46' => '海南','50' => '重庆','51' => '四川','52' => '贵州','53' => '云南','54' => '西藏','61' => '陕西','62' => '甘肃','63' => '青海','64' => '宁夏','65' => '新疆','71' => '台湾','81' => '香港','82' => '澳门','91' => '国外','63' => '青海','64' => '宁夏','65' => '新疆','71' => '台湾','81' => '香港',
  40. '82' => '澳门','91' => '国外' );
  41. /**
  42. * 台湾身份证校验
  43. *
  44. * @var array
  45. */
  46. public static $twFirstCode = array ('A' => 10,'B' => 11,'C' => 12,'D' => 13,'E' => 14,'F' => 15,'G' => 16,'H' => 17,'J' => 18,'K' => 19,'L' => 20,'M' => 21,'N' => 22,'P' => 23,'Q' => 24,'R' => 25,'S' => 26,'T' => 27,'U' => 28,'V' => 29,'X' => 30,'Y' => 31,'W' => 32,'Z' => 33,'I' => 34,'O' => 35 );
  47. /**
  48. * 香港身份证校验
  49. */
  50. public static $hkFirstCode = array ('A' => 1,'B' => 2,'C' => 3,'R' => 18,'U' => 21,'Z' => 26,'X' => 24,'W' => 23,'O' => 15,'N' => 14 );
  51. /**
  52. * 将15位身份证号码转换为18位
  53. *
  54. * @param idCard 15位身份编码
  55. * @return 18位身份编码
  56. */
  57. public static function conver15CardTo18($idCard) {
  58. $idCard18 = "";
  59. if (strlen ( $idCard ) != self::CHINA_ID_MIN_LENGTH) {
  60. return null;
  61. }
  62. if (self::isNum ( $idCard )) {
  63. // 获取出生年月日
  64. $sYear = '19' . substr ( $idCard, 6, 2 );
  65. $idCard18 = substr ( $idCard, 0, 6 ) . $sYear . substr ( $idCard, 8 );
  66. // 转换字符数组
  67. $iArr = str_split ( $idCard18 );
  68. if ($iArr != null) {
  69. $iSum17 = self::getPowerSum ( $iArr );
  70. // 获取校验位
  71. $sVal = self::getCheckCode18 ( $iSum17 );
  72. if (strlen ( $sVal ) > 0) {
  73. $idCard18 .= $sVal;
  74. } else {
  75. return null;
  76. }
  77. }
  78. } else {
  79. return null;
  80. }
  81. return $idCard18;
  82. }
  83. /**
  84. * 验证身份证是否合法
  85. */
  86. public static function validateCard($idCard) {
  87. $card = trim ( $idCard );
  88. if (self::validateIdCard18 ( $card )) {
  89. return true;
  90. }
  91. if (self::validateIdCard15 ( $card )) {
  92. return true;
  93. }
  94. $cardval = self::validateIdCard10 ( $card );
  95. if ($cardval != null) {
  96. if ($cardval [2] == "true") {
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. /**
  103. * 验证18位身份编码是否合法
  104. *
  105. * @param int $idCard 身份编码
  106. * @return boolean 是否合法
  107. */
  108. public static function validateIdCard18($idCard) {
  109. $bTrue = false;
  110. if (strlen ( $idCard ) == self::CHINA_ID_MAX_LENGTH) {
  111. // 前17位
  112. $code17 = substr ( $idCard, 0, 17 );
  113. // 第18位
  114. $code18 = substr ( $idCard, 17, 1 );
  115. if (self::isNum ( $code17 )) {
  116. $iArr = str_split ( $code17 );
  117. if ($iArr != null) {
  118. $iSum17 = self::getPowerSum ( $iArr );
  119. // 获取校验位
  120. $val = self::getCheckCode18 ( $iSum17 );
  121. if (strlen ( $val ) > 0 && $val == $code18) {
  122. $bTrue = true;
  123. }
  124. }
  125. }
  126. }
  127. return $bTrue;
  128. }
  129. /**
  130. * 验证15位身份编码是否合法
  131. *
  132. * @param string $idCard 身份编码
  133. * @return boolean 是否合法
  134. */
  135. public static function validateIdCard15($idCard) {
  136. if (strlen ( $idCard ) != self::CHINA_ID_MIN_LENGTH) {
  137. return false;
  138. }
  139. if (self::isNum ( $idCard )) {
  140. $proCode = substr ( $idCard, 0, 2 );
  141. if (! isset ( self::$cityCodes [$proCode] )) {
  142. return false;
  143. }
  144. // 升到18位
  145. $idCard = self::conver15CardTo18 ( $idCard );
  146. return self::validateIdCard18 ( $idCard );
  147. } else {
  148. return false;
  149. }
  150. return true;
  151. }
  152. /**
  153. * 验证10位身份编码是否合法
  154. *
  155. * @param idCard 身份编码
  156. * @return 身份证信息数组

  157. * [0] - 台湾、澳门、香港 [1] - 性别(男M,女F,未知N) [2] - 是否合法(合法true,不合法false)
  158. * 若不是身份证件号码则返回null
  159. *

  160. */
  161. public static function validateIdCard10($idCard) {
  162. $info = array ();
  163. $card = str_replace ( "[\\(|\\)]", "", $card );
  164. $len = strlen ( $card );
  165. if ($len != 8 && $len != 9 && $len != 10) {
  166. return null;
  167. }
  168. if (0 < preg_match ( "/^[a-zA-Z][0-9]{9}$/", $idCard )) { // 台湾
  169. $info [0] = "台湾";
  170. $char2 = substr ( $idCard, 1, 1 );
  171. if ($char2 == "1") {
  172. $info [1] = "M";
  173. } else if ($char2 == "2") {
  174. $info [1] = "F";
  175. } else {
  176. $info [1] = "N";
  177. $info [2] = "false";
  178. return $info;
  179. }
  180. $info [2] = self::validateTWCard ( $idCard ) ? "true" : "false";
  181. } else if (0 < preg_match ( "/^[1|5|7][0-9]{6}\\(?[0-9A-Z]\\)?$/", $idCard )) { // 澳门
  182. $info [0] = "澳门";
  183. $info [1] = "N";
  184. // TODO
  185. } else if (0 < preg_match ( "/^[A-Z]{1,2}[0-9]{6}\\(?[0-9A]\\)?$/", $idCard )) { // 香港
  186. $info [0] = "香港";
  187. $info [1] = "N";
  188. $info [2] = self::validateHKCard ( $idCard ) ? "true" : "false";
  189. } else {
  190. return null;
  191. }
  192. return info;
  193. }
  194. /**
  195. * 验证台湾身份证号码
  196. *
  197. * @param string idCard 身份证号码
  198. * @return 验证码是否符合
  199. */
  200. public static function validateTWCard($idCard) {
  201. $start = substr ( $idCard, 0, 1 );
  202. $mid = substr ( $idCard, 1, 8 );
  203. $end = substr ( $idCard, 9, 1 );
  204. $iStart = self::$twFirstCode ['start'];
  205. $sum = $iStart / 10 + ($iStart % 10) * 9;
  206. $chars = str_split ( $mid );
  207. $iflag = 8;
  208. foreach ( $chars as $c ) {
  209. $sum = $sum + $c + "" * $iflag;
  210. $iflag --;
  211. }
  212. return ($sum % 10 == 0 ? 0 : (10 - $sum % 10)) == $end ? true : false;
  213. }
  214. /**
  215. * 验证香港身份证号码(存在Bug,部份特殊身份证无法检查)
  216. *

  217. * 身份证前2位为英文字符,如果只出现一个英文字符则表示第一位是空格,对应数字58 前2位英文字符A-Z分别对应数字10-35
  218. * 最后一位校验码为0-9的数字加上字符"A","A"代表10
  219. *

  220. *

  221. * 将身份证号码全部转换为数字,分别对应乘9-1相加的总和,整除11则证件号码有效
  222. *

  223. *
  224. * @param idCard 身份证号码
  225. * @return 验证码是否符合
  226. */
  227. public static function validateHKCard($idCard) {
  228. $card = str_replace ( "[\\(|\\)]", "", $card );
  229. $sum = 0;
  230. if (strlen ( $card ) == 9) {
  231. $card0_arr = str_split ( strtoupper ( substr ( $card, 0, 1 ) ) );
  232. $card1_arr = str_split ( strtoupper ( substr ( $card, 1, 1 ) ) );
  233. $sum = ($card0_arr [0] - 55) * 9 . ($card1_arr [0] - 55) * 8;
  234. $card = substr ( $card, 1, 8 );
  235. } else {
  236. $card0_arr = str_split ( strtoupper ( substr ( $card, 0, 1 ) ) );
  237. $sum = 522 + ($card0_arr [0] - 55) * 8;
  238. }
  239. $mid = substr ( $card, 1, 6 );
  240. $end = substr ( $card, 7, 1 );
  241. $chars = str_split ( $mid );
  242. $iflag = 7;
  243. foreach ( $chars as $c ) {
  244. $sum = $sum + $c + "" * $iflag;
  245. $iflag --;
  246. }
  247. if (strtoupper ( $end ) == "A") {
  248. $sum = $sum + 10;
  249. } else {
  250. $sum = $sum + $end;
  251. }
  252. return ($sum % 11 == 0) ? true : false;
  253. }
  254. /**
  255. * 根据身份编号获取年龄
  256. *
  257. * @param string idCard 身份编号
  258. * @return 年龄
  259. */
  260. public static function getAgeByIdCard($idCard) {
  261. $iAge = 0;
  262. if (strlen ( $idCard ) == self::CHINA_ID_MIN_LENGTH) {
  263. $idCard = self::conver15CardTo18 ( $idCard );
  264. }
  265. $year = substr ( $idCard, 6, 4 );
  266. $iCurrYear = date ( 'Y', time () );
  267. $iAge = $iCurrYear - $year;
  268. return $iAge;
  269. }
  270. /**
  271. * 根据身份编号获取生日天
  272. *
  273. * @param string $idCard 身份编号
  274. * @return NULL string
  275. */
  276. public static function getDateByIdCard($idCard) {
  277. $len = strlen ( $idCard );
  278. if ($len < self::CHINA_ID_MIN_LENGTH) {
  279. return null;
  280. } else if ($len == CHINA_ID_MIN_LENGTH) {
  281. $idCard = self::conver15CardTo18 ( $idCard );
  282. }
  283. return substr ( $idCard, 12, 2 );
  284. }
  285. /**
  286. * 根据身份编号获取性别
  287. *
  288. * @param string $idCard 身份编号
  289. * @return 性别(M-男,F-女,N-未知)
  290. */
  291. public static function getGenderByIdCard($idCard) {
  292. $sGender = "N";
  293. if (strlen ( $idCard ) == self::CHINA_ID_MIN_LENGTH) {
  294. $idCard = self::conver15CardTo18 ( $idCard );
  295. }
  296. $sCardNum = substr ( $idCard, 16, 1 );
  297. if (( int ) $sCardNum % 2 != 0) {
  298. $sGender = "M";
  299. } else {
  300. $sGender = "F";
  301. }
  302. return $sGender;
  303. }
  304. /**
  305. * 根据身份编号获取户籍省份
  306. *
  307. * @param string $idCard 身份编号
  308. * @return string
  309. */
  310. public static function getProvinceByIdCard($idCard) {
  311. $len = strlen ( $idCard );
  312. $sProvince = null;
  313. $sProvinNum = "";
  314. if ($len == self::CHINA_ID_MIN_LENGTH || $len == self::CHINA_ID_MAX_LENGTH) {
  315. $sProvinNum = substr ( $idCard, 0, 2 );
  316. }
  317. $sProvince = self::$cityCodes [$sProvinNum];
  318. return $sProvince;
  319. }
  320. /**
  321. * 数字验证
  322. *
  323. * @param int $val
  324. */
  325. public static function isNum($val) {
  326. return $val == null || $val == "" ? false : 0 < preg_match ( '/^[0-9]*$/', $val );
  327. }
  328. /**
  329. * 验证小于当前日期 是否有效
  330. *
  331. * @param int $iYear 待验证日期(年)
  332. * @param int $iMonth 待验证日期(月 1-12)
  333. * @param int $iDate 待验证日期(日)
  334. * @return 是否有效
  335. */
  336. public static function valiDate($iYear, $iMonth, $iDate) {
  337. $year = date ( 'Y', time () );
  338. if ($iYear < self::MIN || $iYear >= $year) {
  339. return false;
  340. }
  341. if ($iMonth < 1 || $iMonth > 12) {
  342. return false;
  343. }
  344. switch ($iMonth) {
  345. case 4 :
  346. case 6 :
  347. case 9 :
  348. case 11 :
  349. $datePerMonth = 30;
  350. break;
  351. case 2 :
  352. $dm = (($iYear % 4 == 0 && $iYear % 100 != 0) || ($iYear % 400 == 0)) && ($iYear > self::MIN && $iYear < $year);
  353. $datePerMonth = $dm ? 29 : 28;
  354. break;
  355. default :
  356. $datePerMonth = 31;
  357. }
  358. return ($iDate >= 1) && ($iDate <= $datePerMonth);
  359. }
  360. /**
  361. * 将身份证的每位和对应位的加权因子相乘之后,再得到和值
  362. *
  363. * @param array $iArr
  364. * @return 身份证编码。
  365. */
  366. private static function getPowerSum($iArr) {
  367. $iSum = 0;
  368. $power_len = count ( self::$power );
  369. $iarr_len = count ( $iArr );
  370. if ($power_len == $iarr_len) {
  371. for($i = 0; $i < $iarr_len; $i ++) {
  372. for($j = 0; $j < $power_len; $j ++) {
  373. if ($i == $j) {
  374. $iSum = $iSum + $iArr [$i] * self::$power [$j];
  375. }
  376. }
  377. }
  378. }
  379. return $iSum;
  380. }
  381. /**
  382. * 将power和值与11取模获得余数进行校验码判断
  383. *
  384. * @param int $iSum
  385. * @return 校验位
  386. */
  387. private static function getCheckCode18($iSum) {
  388. $sCode = "";
  389. switch ($iSum % 11) {
  390. case 10 :
  391. $sCode = "2";
  392. break;
  393. case 9 :
  394. $sCode = "3";
  395. break;
  396. case 8 :
  397. $sCode = "4";
  398. break;
  399. case 7 :
  400. $sCode = "5";
  401. break;
  402. case 6 :
  403. $sCode = "6";
  404. break;
  405. case 5 :
  406. $sCode = "7";
  407. break;
  408. case 4 :
  409. $sCode = "8";
  410. break;
  411. case 3 :
  412. $sCode = "9";
  413. break;
  414. case 2 :
  415. $sCode = "x";
  416. break;
  417. case 1 :
  418. $sCode = "0";
  419. break;
  420. case 0 :
  421. $sCode = "1";
  422. break;
  423. }
  424. return $sCode;
  425. }
  426. }

人气教程排行