search
HomeBackend DevelopmentPHP TutorialPHP's ID card number tool class is translated into Java

Realize the conversion from 15-digit to 18-digit ID numbers, and verify the Mainland China Resident ID Card, Hong Kong Resident ID Card, Macau ID Card and Taiwan ID Card.
  1. /**
  2. * ID card tools
  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. * Minimum length of Chinese citizen ID number.
  12. */
  13. const CHINA_ID_MIN_LENGTH = 15;
  14. /**
  15. * Maximum length of Chinese citizen ID number .
  16. */
  17. const CHINA_ID_MAX_LENGTH = 18;
  18. /**
  19. * Minimum years
  20. */
  21. const MIN = 1930;
  22. /**
  23. *Province and municipality code table
  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. * Weighting factor per bit
  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. * The 18th digit of the check code
  32. */
  33. public static $verifyCode = array ("1","0","X","9","8","7","6","5","4","3","2" );
  34. /**
  35. *Domestic ID card verification
  36. */
  37. public static $cityCodes = array ('11' => '北京' );
  38. /**
  39. * Convert 15-digit ID number to 18 digits
  40. *
  41. * @param idCard 15-digit identity code
  42. * @return 18-digit identity code
  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. * Verify whether the ID card is legal
  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. * Verify whether the 18-digit identity code is legal
  85. *
  86. * @param int $idCard identity code
  87. * @return boolean whether it is legal
  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. * Verify whether the 15-digit identity code is legal
  112. *
  113. * @param string $idCard identity code
  114. * @return boolean whether it is legal
  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. * Get age based on ID number
  135. *
  136. * @param string idCard ID number
  137. * @return age
  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. * Get the birthday based on the identity number
  151. *
  152. * @param string $idCard identity number
  153. * @return NULL string
  154. */
  155. public static function getDateByIdCard($idCard) {
  156. $len = strlen ( $idCard );
  157. if ($len return null;
  158. } else if ($len == CHINA_ID_MIN_LENGTH) {
  159. $idCard = self::conver15CardTo18 ( $idCard );
  160. }
  161. return substr ( $idCard, 12, 2 );
  162. }
  163. /**
  164. * Get gender based on identity number
  165. *
  166. * @param string $idCard identity number
  167. * @return gender (M-male, F-female, N-unknown)
  168. */
  169. public static function getGenderByIdCard($idCard) {
  170. $sGender = "N";
  171. if (strlen ( $idCard ) == self::CHINA_ID_MIN_LENGTH) {
  172. $idCard = self::conver15CardTo18 ( $idCard );
  173. }
  174. $sCardNum = substr ( $idCard, 16, 1 );
  175. if (( int ) $sCardNum % 2 != 0) {
  176. $sGender = "M";
  177. } else {
  178. $sGender = "F";
  179. }
  180. return $sGender;
  181. }
  182. /**
  183. * Get the province of household registration based on the identity number
  184. *
  185. * @param string $idCard identity number
  186. * @return string
  187. */
  188. public static function getProvinceByIdCard($idCard) {
  189. $len = strlen ( $idCard );
  190. $sProvince = null;
  191. $sProvinNum = "";
  192. if ($len == self::CHINA_ID_MIN_LENGTH || $len == self::CHINA_ID_MAX_LENGTH) {
  193. $sProvinNum = substr ( $idCard, 0, 2 );
  194. }
  195. $sProvince = self::$cityCodes [$sProvinNum];
  196. return $sProvince;
  197. }
  198. /**
  199. *Number verification
  200. *
  201. * @param int $val
  202. */
  203. public static function isNum($val) {
  204. return $val == null || $val == "" ? false : 0 }
  205. /**
  206. * Verify whether a date smaller than the current date is valid
  207. *
  208. * @param int $iYear Date to be verified (year)
  209. * @param int $iMonth Date to be verified (month 1-12)
  210. * @param int $iDate Date to be verified ( Day)
  211. * @return Is it valid
  212. */
  213. public static function valiDate($iYear, $iMonth, $iDate) {
  214. $year = date ( 'Y', time () );
  215. if ($iYear = $year) {
  216. return false;
  217. }
  218. if ($iMonth 12) {
  219. return false;
  220. }
  221. switch ($iMonth) {
  222. case 4 :
  223. case 6 :
  224. case 9 :
  225. case 11 :
  226. $datePerMonth = 30;
  227. break;
  228. case 2 :
  229. $dm = (($iYear % 4 == 0 && $iYear % 100 != 0) || ($iYear % 400 == 0)) && ($iYear > self::MIN && $iYear $datePerMonth = $dm ? 29 : 28;
  230. break;
  231. default :
  232. $datePerMonth = 31;
  233. }
  234. return ($iDate >= 1) && ($iDate }
  235. /**
  236. * After multiplying each bit of the ID card and the weighting factor of the corresponding bit, the sum value is obtained
  237. *
  238. * @param array $iArr
  239. * @return ID code.
  240. */
  241. private static function getPowerSum($iArr) {
  242. $iSum = 0;
  243. $power_len = count ( self::$power );
  244. $iarr_len = count ( $iArr );
  245. if ($power_len == $iarr_len) {
  246. for($i = 0; $i for($j = 0; $j if ($i == $j) {
  247. $iSum = $iSum + $iArr [$i] * self::$power [$j];
  248. }
  249. }
  250. }
  251. }
  252. return $iSum;
  253. }
  254. /**
  255. * Take the power sum value modulo 11 to get the remainder for check digit judgment
  256. *
  257. * @param int $iSum
  258. * @return check digit
  259. */
  260. private static function getCheckCode18($iSum) {
  261. $sCode = "";
  262. switch ($iSum % 11) {
  263. case 10 :
  264. $sCode = "2";
  265. break;
  266. case 9 :
  267. $sCode = "3";
  268. break;
  269. case 8 :
  270. $sCode = "4";
  271. break;
  272. case 7 :
  273. $sCode = "5";
  274. break;
  275. case 6 :
  276. $sCode = "6";
  277. break;
  278. case 5 :
  279. $sCode = "7";
  280. break;
  281. case 4 :
  282. $sCode = "8";
  283. break;
  284. case 3 :
  285. $sCode = "9";
  286. break;
  287. case 2 :
  288. $sCode = "x";
  289. break;
  290. case 1 :
  291. $sCode = "0";
  292. break;
  293. case 0 :
  294. $sCode = "1";
  295. break;
  296. }
  297. return $sCode;
  298. }
  299. }
复制代码
  1. /**
  2. * ID card tools
  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. * Minimum length of Chinese citizen ID number.
  12. */
  13. const CHINA_ID_MIN_LENGTH = 15;
  14. /**
  15. * Maximum length of Chinese citizen ID number .
  16. */
  17. const CHINA_ID_MAX_LENGTH = 18;
  18. /**
  19. * Minimum years
  20. */
  21. const MIN = 1930;
  22. /**
  23. *Province and municipality code table
  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. * Weighting factor per bit
  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. * The 18th digit of the check code
  32. */
  33. public static $verifyCode = array ("1","0","X","9","8","7","6","5","4","3","2" );
  34. /**
  35. *Domestic ID card verification
  36. */
  37. public static $cityCodes = array ('11' => 'Beijing','12' => 'Tianjin','13' => 'Hebei','14' => 'Shanxi','15' => 'Inner Mongolia','21' => 'Liaoning','22' => 'Jilin','23' => 'Heilongjiang','31' => 'Shanghai','32' => 'Jiangsu','33' => 'Zhejiang','34' => 'Anhui','35' => 'Fujian','36' => 'Jiangxi','37' => 'Shandong','41' => 'Henan','42' => 'Hubei','43' => 'Hunan','44' => 'Guangdong','45' => 'Guangxi',
  38. '46' => 'Hainan','50' => 'Chongqing','51' => 'Sichuan','52 ' => 'Guizhou','53' => 'Yunnan','54' => 'Tibet','61' => 'Shaanxi','62' => 'Gansu','15 ' => 'Inner Mongolia','21' => 'Liaoning','22' => 'Jilin','23' => 'Heilongjiang','31' => 'Shanghai','32 ' => 'Jiangsu','33' => 'Zhejiang','34' => 'Anhui','35' => 'Fujian','36' => 'Jiangxi','37 ' => 'Shandong','41' => 'Henan','42' => 'Hubei','43' => 'Hunan',
  39. '44' => 'Guangdong',' 45' => 'Guangxi','46' => 'Hainan','50' => 'Chongqing','51' => 'Sichuan','52' => 'Guizhou',' 53' => 'Yunnan','54' => 'Tibet','61' => 'Shaanxi','62' => 'Gansu','63' => 'Qinghai',' 64' => 'Ningxia','65' => 'Xinjiang','71' => 'Taiwan','81' => 'Hong Kong','82' => 'Macau',' 91' => 'Overseas','63' => 'Qinghai','64' => 'Ningxia','65' => 'Xinjiang','71' => 'Taiwan',' 81' => 'Hong Kong',
  40. '82' => 'Macau','91' => 'Abroad' );
  41. /**
  42. *Taiwan ID card verification
  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. * Hong Kong ID card verification
  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. * Convert 15-digit ID number to 18 digits
  53. *
  54. * @param idCard 15-digit identity code
  55. * @return 18-digit identity code
  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. // Get the year, month and day of birth
  64. $sYear = '19' . substr ( $idCard, 6, 2 );
  65. $idCard18 = substr ( $idCard, 0, 6 ) . $sYear . substr ( $idCard, 8 );
  66. // Convert character array
  67. $iArr = str_split ( $idCard18 );
  68. if ($iArr != null) {
  69. $iSum17 = self::getPowerSum ( $iArr );
  70. // Get the check digit
  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. * Verify whether the ID card is legal
  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. * Verify whether the 18-digit identity code is legal
  104. *
  105. * @param int $idCard identity code
  106. * @return boolean whether it is legal
  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. * Verify whether the 15-digit identity code is legal
  131. *
  132. * @param string $idCard identity code
  133. * @return boolean whether it is legal
  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. * Verify whether the 10-digit identity code is legal
  154. *
  155. * @param idCard identity code
  156. * @return ID card information array

  157. * [0] - Taiwan, Macau, Hong Kong [1] - Gender (Male M, Female F, unknown N) [2] - Is it legal (legal true, not legal false)
  158. * If it is not an ID number, return 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 $info [0] = "台湾";
  169. $char2 = substr ( $idCard, 1, 1 );
  170. if ($char2 == "1") {
  171. $info [1] = "M";
  172. } else if ($char2 == "2") {
  173. $info [1] = "F";
  174. } else {
  175. $info [1] = "N";
  176. $info [2] = "false";
  177. return $info;
  178. }
  179. $info [2] = self::validateTWCard ( $idCard ) ? "true" : "false";
  180. } else if (0 $info [0] = "澳门";
  181. $info [1] = "N";
  182. // TODO
  183. } else if (0 $info [0] = "香港";
  184. $info [1] = "N";
  185. $info [2] = self::validateHKCard ( $idCard ) ? "true" : "false";
  186. } else {
  187. return null;
  188. }
  189. return info;
  190. }
  191. /**
  192. * Verify Taiwan ID card number
  193. *
  194. * @param string idCard ID card number
  195. * @return Verification code is consistent
  196. */
  197. public static function validateTWCard($idCard) {
  198. $start = substr ( $idCard, 0, 1 );
  199. $mid = substr ( $idCard, 1, 8 );
  200. $end = substr ( $idCard, 9, 1 );
  201. $iStart = self::$twFirstCode ['start'];
  202. $sum = $iStart / 10 + ($iStart % 10) * 9;
  203. $chars = str_split ( $mid );
  204. $iflag = 8;
  205. foreach ( $chars as $c ) {
  206. $sum = $sum + $c + "" * $iflag;
  207. $iflag --;
  208. }
  209. return ($sum % 10 == 0 ? 0 : (10 - $sum % 10)) == $end ? true : false;
  210. }
  211. /**
  212. * Verify Hong Kong ID card number (there is a bug, some special ID cards cannot be checked)
  213. *

  214. * The first 2 digits of the ID card are English characters. If only one English character appears, it means the first digit is a space. Corresponding to the number 58, the first two English characters A-Z correspond to the numbers 10-35
  215. respectively * The last digit of the check code is 0-9 plus the character "A", "A" represents 10
  216. *
  217. *

    * @return Whether the verification code matches

  218. */
  219. public static function validateHKCard($idCard) {
  220. $card = str_replace ( "[\(|\)]", "", $card );
  221. $sum = 0;
  222. if (strlen ( $card ) == 9) {
  223. $card0_arr = str_split ( strtoupper ( substr ( $card, 0, 1 ) ) );
  224. $card1_arr = str_split ( strtoupper ( substr ( $card, 1, 1 ) ) );
  225. $sum = ($card0_arr [0] - 55) * 9 . ($card1_arr [0] - 55) * 8;
  226. $card = substr ( $card, 1, 8 );
  227. } else {
  228. $card0_arr = str_split ( strtoupper ( substr ( $card, 0, 1 ) ) );
  229. $sum = 522 + ($card0_arr [0] - 55) * 8;
  230. }
  231. $mid = substr ( $card, 1, 6 );
  232. $end = substr ( $card, 7, 1 );
  233. $chars = str_split ( $mid );
  234. $iflag = 7;
  235. foreach ( $chars as $c ) {
  236. $sum = $sum + $c + "" * $iflag;
  237. $iflag --;
  238. }
  239. if (strtoupper ( $end ) == "A") {
  240. $sum = $sum + 10;
  241. } else {
  242. $sum = $sum + $end;
  243. }
  244. return ($sum % 11 == 0) ? true : false;
  245. }
  246. /**
  247. * Get age based on ID number
  248. *
  249. * @param string idCard ID number
  250. * @return age
  251. */
  252. public static function getAgeByIdCard($idCard) {
  253. $iAge = 0;
  254. if (strlen ( $idCard ) == self::CHINA_ID_MIN_LENGTH) {
  255. $idCard = self::conver15CardTo18 ( $idCard );
  256. }
  257. $year = substr ( $idCard, 6, 4 );
  258. $iCurrYear = date ( 'Y', time () );
  259. $iAge = $iCurrYear - $year;
  260. return $iAge;
  261. }
  262. /**
  263. * Get the birthday based on the identity number
  264. *
  265. * @param string $idCard identity number
  266. * @return NULL string
  267. */
  268. public static function getDateByIdCard($idCard) {
  269. $len = strlen ( $idCard );
  270. if ($len return null;
  271. } else if ($len == CHINA_ID_MIN_LENGTH) {
  272. $idCard = self::conver15CardTo18 ( $idCard );
  273. }
  274. return substr ( $idCard, 12, 2 );
  275. }
  276. /**
  277. * Get gender based on identity number
  278. *
  279. * @param string $idCard identity number
  280. * @return gender (M-male, F-female, N-unknown)
  281. */
  282. public static function getGenderByIdCard($idCard) {
  283. $sGender = "N";
  284. if (strlen ( $idCard ) == self::CHINA_ID_MIN_LENGTH) {
  285. $idCard = self::conver15CardTo18 ( $idCard );
  286. }
  287. $sCardNum = substr ( $idCard, 16, 1 );
  288. if (( int ) $sCardNum % 2 != 0) {
  289. $sGender = "M";
  290. } else {
  291. $sGender = "F";
  292. }
  293. return $sGender;
  294. }
  295. /**
  296. * Get the province of household registration based on the identity number
  297. *
  298. * @param string $idCard identity number
  299. * @return string
  300. */
  301. public static function getProvinceByIdCard($idCard) {
  302. $len = strlen ( $idCard );
  303. $sProvince = null;
  304. $sProvinNum = "";
  305. if ($len == self::CHINA_ID_MIN_LENGTH || $len == self::CHINA_ID_MAX_LENGTH) {
  306. $sProvinNum = substr ( $idCard, 0, 2 );
  307. }
  308. $sProvince = self::$cityCodes [$sProvinNum];
  309. return $sProvince;
  310. }
  311. /**
  312. *Number verification
  313. *
  314. * @param int $val
  315. */
  316. public static function isNum($val) {
  317. return $val == null || $val == "" ? false : 0 }
  318. /**
  319. * Verify whether a date smaller than the current date is valid
  320. *
  321. * @param int $iYear Date to be verified (year)
  322. * @param int $iMonth Date to be verified (month 1-12)
  323. * @param int $iDate Date to be verified ( Day)
  324. * @return Is it valid
  325. */
  326. public static function valiDate($iYear, $iMonth, $iDate) {
  327. $year = date ( 'Y', time () );
  328. if ($iYear = $year) {
  329. return false;
  330. }
  331. if ($iMonth 12) {
  332. return false;
  333. }
  334. switch ($iMonth) {
  335. case 4 :
  336. case 6 :
  337. case 9 :
  338. case 11 :
  339. $datePerMonth = 30;
  340. break;
  341. case 2 :
  342. $dm = (($iYear % 4 == 0 && $iYear % 100 != 0) || ($iYear % 400 == 0)) && ($iYear > self::MIN && $iYear $datePerMonth = $dm ? 29 : 28;
  343. break;
  344. default :
  345. $datePerMonth = 31;
  346. }
  347. return ($iDate >= 1) && ($iDate }
  348. /**
  349. * 将身份证的每位和对应位的加权因子相乘之后,再得到和值
  350. *
  351. * @param array $iArr
  352. * @return 身份证编码。
  353. */
  354. private static function getPowerSum($iArr) {
  355. $iSum = 0;
  356. $power_len = count ( self::$power );
  357. $iarr_len = count ( $iArr );
  358. if ($power_len == $iarr_len) {
  359. for($i = 0; $i for($j = 0; $j if ($i == $j) {
  360. $iSum = $iSum + $iArr [$i] * self::$power [$j];
  361. }
  362. }
  363. }
  364. }
  365. return $iSum;
  366. }
  367. /**
  368. * Take the power sum value modulo 11 to get the remainder for check digit judgment
  369. *
  370. * @param int $iSum
  371. * @return check digit
  372. */
  373. private static function getCheckCode18($iSum) {
  374. $sCode = "";
  375. switch ($iSum % 11) {
  376. case 10 :
  377. $sCode = "2";
  378. break;
  379. case 9 :
  380. $sCode = "3";
  381. break;
  382. case 8 :
  383. $sCode = "4";
  384. break;
  385. case 7 :
  386. $sCode = "5";
  387. break;
  388. case 6 :
  389. $sCode = "6";
  390. break;
  391. case 5 :
  392. $sCode = "7";
  393. break;
  394. case 4 :
  395. $sCode = "8";
  396. break;
  397. case 3 :
  398. $sCode = "9";
  399. break;
  400. case 2 :
  401. $sCode = "x";
  402. break;
  403. case 1 :
  404. $sCode = "0";
  405. break;
  406. case 0 :
  407. $sCode = "1";
  408. break;
  409. }
  410. return $sCode;
  411. }
  412. }
复制代码


Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.