0
0

Idcard.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace tools;
  3. //身份证验证类
  4. class Idcard {
  5. public $aWeight = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]; //十七位数字本体码权重
  6. public $aValidate = ['1','0','X','9','8','7','6','5','4','3','2']; //mod11,对应校验码字符值
  7. public $birthday;//出生年月
  8. /**
  9. * 验证出生日期
  10. */
  11. public function isChinaIDCardDate($iY, $iM, $iD)
  12. {
  13. $iDate = $iY . '-' . $iM . '-' . $iD;
  14. $rPattern = '/^(([0-9]{2})|(19[0-9]{2})|(20[0-9]{2}))-((0[1-9]{1})|(1[012]{1}))-((0[1-9]{1})|(1[0-9]{1})|(2[0-9]{1})|3[01]{1})$/';
  15. if(preg_match($rPattern, $iDate, $arr)){
  16. $this->birthday = $iDate;
  17. return true;
  18. }
  19. return false;
  20. }
  21. /**
  22. * 根据身份证号前17位, 算出识别码
  23. */
  24. public function getValidateCode($id)
  25. {
  26. $id = strtoupper($id);
  27. $id17 = substr($id,0,17);
  28. $sum = 0;
  29. $len = strlen($id17);
  30. for ($i=0; $i<$len; $i++){
  31. $sum += $id17[$i] * $this->aWeight[$i];
  32. }
  33. $mode = $sum % 11;
  34. return $this->aValidate[$mode];
  35. }
  36. /**
  37. * 验证身份证号
  38. */
  39. public function isChinaIDCard($id)
  40. {
  41. $id = strtoupper($id);
  42. if(!$id){
  43. return false;
  44. }
  45. $len = strlen($id);
  46. if($len == 18){
  47. if (!$this->isChinaIDCardDate(substr($id,6,4), substr($id,10,2), substr($id,12,2))){
  48. return false;
  49. }
  50. $code = $this->getValidateCode($id);
  51. if (strtoupper($code) == substr($id,17,1)){
  52. return true;
  53. }
  54. return false;
  55. }
  56. else if($len == 15)
  57. {
  58. if(!$this->isChinaIDCardDate('19'.substr($id,6,2),substr($id,8,2),substr($id,10,2))){
  59. return false;
  60. }
  61. if(!is_numeric($id)){
  62. return false;
  63. }
  64. return true;
  65. }
  66. return false;
  67. }
  68. /**
  69. * 根据身份证号,自动返回对应的性别
  70. */
  71. public function getChinaIDCardSex($cid)
  72. {
  73. $cid = strtoupper($cid);
  74. $sexint = (int)substr($cid,16,1);
  75. return $sexint % 2 === 0 ? '女' : '男';
  76. }
  77. /**
  78. * 根据身份证号,自动返回对应的星座
  79. */
  80. public function getChinaIDCardXZ($cid)
  81. {
  82. $cid = strtoupper($cid);
  83. $bir = substr($cid,10,4);
  84. $month = (int)substr($bir,0,2);
  85. $day = (int)substr($bir,2);
  86. $strValue = '';
  87. if(($month == 1 && $day <= 21) || ($month == 2 && $day <= 19)) {
  88. $strValue = "水瓶座";
  89. }else if(($month == 2 && $day > 20) || ($month == 3 && $day <= 20)) {
  90. $strValue = "双鱼座";
  91. }else if (($month == 3 && $day > 20) || ($month == 4 && $day <= 20)) {
  92. $strValue = "白羊座";
  93. }else if (($month == 4 && $day > 20) || ($month == 5 && $day <= 21)) {
  94. $strValue = "金牛座";
  95. }else if (($month == 5 && $day > 21) || ($month == 6 && $day <= 21)) {
  96. $strValue = "双子座";
  97. }else if (($month == 6 && $day > 21) || ($month == 7 && $day <= 22)) {
  98. $strValue = "巨蟹座";
  99. }else if (($month == 7 && $day > 22) || ($month == 8 && $day <= 23)) {
  100. $strValue = "狮子座";
  101. }else if (($month == 8 && $day > 23) || ($month == 9 && $day <= 23)) {
  102. $strValue = "处女座";
  103. }else if (($month == 9 && $day > 23) || ($month == 10 && $day <= 23)) {
  104. $strValue = "天秤座";
  105. }else if (($month == 10 && $day > 23) || ($month == 11 && $day <= 22)) {
  106. $strValue = "天蝎座";
  107. }else if (($month == 11 && $day > 22) || ($month == 12 && $day <= 21)) {
  108. $strValue = "射手座";
  109. }else if (($month == 12 && $day > 21) || ($month == 1 && $day <= 20)) {
  110. $strValue = "魔羯座";
  111. }
  112. return $strValue;
  113. }
  114. /**
  115. * 根据身份证号,自动返回对应的生肖
  116. */
  117. public function getChinaIDCardSX($cid)
  118. {
  119. $cid = strtoupper($cid);
  120. $start = 1901;
  121. $end = $end = (int)substr($cid,6,4);
  122. $x = ($start - $end) % 12;
  123. $value = "";
  124. if($x == 1 || $x == -11){$value = "鼠";}
  125. if($x == 0) {$value = "牛";}
  126. if($x == 11 || $x == -1){$value = "虎";}
  127. if($x == 10 || $x == -2){$value = "兔";}
  128. if($x == 9 || $x == -3){$value = "龙";}
  129. if($x == 8 || $x == -4){$value = "蛇";}
  130. if($x == 7 || $x == -5){$value = "马";}
  131. if($x == 6 || $x == -6){$value = "羊";}
  132. if($x == 5 || $x == -7){$value = "猴";}
  133. if($x == 4 || $x == -8){$value = "鸡";}
  134. if($x == 3 || $x == -9){$value = "狗";}
  135. if($x == 2 || $x == -10){$value = "猪";}
  136. return $value;
  137. }
  138. /**
  139. * 根据身份证号,自动返回对应的省、自治区、直辖市代
  140. */
  141. public function getProvince($id){
  142. $id = strtoupper($id);
  143. $index = substr($id,0,2);
  144. $area = array(
  145. 11 => "北京", 12 => "天津", 13 => "河北", 14 => "山西", 15 => "内蒙古", 21 => "辽宁",
  146. 22 => "吉林", 23 => "黑龙江", 31 => "上海", 32 => "江苏", 33 => "浙江", 34 => "安徽",
  147. 35 => "福建", 36 => "江西", 37 => "山东", 41 => "河南", 42 => "湖北", 43 => "湖南",
  148. 44 => "广东", 45 => "广西", 46 => "海南", 50 => "重庆", 51 => "四川", 52 => "贵州",
  149. 53 => "云南", 54 => "西藏", 61 => "陕西", 62 => "甘肃", 63 => "青海", 64 => "宁夏",
  150. 65 => "新疆", 71 => "台湾", 81 => "香港", 82 => "澳门", 91 => "国外"
  151. );
  152. return $area[$index];
  153. }
  154. // 获取生日
  155. public function getBirthday($idcard){
  156. $idcard = strtoupper($idcard);
  157. if(!$this->isChinaIDCard($idcard) || strlen($idcard) != 18){
  158. return false;
  159. }
  160. $bir = substr($idcard, 6, 8);
  161. $year = (int)substr($bir, 0, 4);
  162. $month = (int)substr($bir, 4, 2);
  163. $day = (int)substr($bir, 6, 2);
  164. return date('Y-m-d',strtotime($year . "-" . $month . "-" . $day));
  165. }
  166. }