IdCardInfoExtractorHelper.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2017/11/29/029
  6. * Time: 10:10
  7. */
  8. namespace backend\helpers;
  9. class IdCardInfoExtractorHelper
  10. {
  11. // 省份
  12. private $province;
  13. // 城市
  14. private $city;
  15. // 区县
  16. private $region;
  17. // 年份
  18. private $year;
  19. // 月份
  20. private $month;
  21. // 日期
  22. private $day;
  23. // 性别
  24. private $gender;
  25. // 出生日期
  26. private $birthday;
  27. // 年龄
  28. private $age;
  29. private $cityCode = [
  30. '11' => '北京',
  31. '12' => '天津',
  32. '13' => '河北',
  33. '14' => '山西',
  34. '15' => '内蒙古',
  35. '21' => '辽宁',
  36. '22' => '吉林',
  37. '23' => '黑龙江',
  38. '31' => '上海',
  39. '32' => '江苏',
  40. '33' => '浙江',
  41. '34' => '安徽',
  42. '35' => '福建',
  43. '36' => '江西',
  44. '37' => '山东',
  45. '41' => '河南',
  46. '42' => '湖北',
  47. '43' => '湖南',
  48. '44' => '广东',
  49. '45' => '广西',
  50. '46' => '海南',
  51. '50' => '重庆',
  52. '51' => '四川',
  53. '52' => '贵州',
  54. '53' => '云南',
  55. '54' => '西藏',
  56. '61' => '陕西',
  57. '62' => '甘肃',
  58. '63' => '青海',
  59. '64' => '宁夏',
  60. '65' => '新疆',
  61. '71' => '台湾',
  62. '81' => '香港',
  63. '82' => '澳门',
  64. '91' => '国外',
  65. ];
  66. private $validator = null;
  67. public function __construct($idcard)
  68. {
  69. try {
  70. $this->validator = new IdcardValidatorHelper();
  71. if ($this->validator->checkIdCard($idcard)) {
  72. if (strlen($idcard) == 15) {
  73. $idcard = $this->validator->convertIdcarBy15bit($idcard);
  74. }
  75. // 获取省份
  76. $provinceId = substr($idcard, 0, 2);
  77. foreach ($this->cityCode as $key=>$item) {
  78. if ($provinceId == $key) {
  79. $this->province = $item;
  80. break;
  81. }
  82. }
  83. // 获取性别
  84. $id17 = substr($idcard, 16, 1);
  85. if ($id17 % 2 != 0) {
  86. $this->gender = "男";
  87. } else {
  88. $this->gender = "女";
  89. }
  90. // 获取出生日期
  91. $birthday = substr($idcard, 6, 8);
  92. $d = new \DateTime($birthday);
  93. $dd = $d->format('Y-m-d H:i:s');
  94. $this->birthday = $dd;
  95. $this->year = substr($dd, 0, 4);
  96. $this->month = substr($dd, 4, 2);
  97. $this->day = substr($dd, 6, 2);
  98. $this->age = (int)date('Y') - $this->year;
  99. }
  100. } catch (\Exception $e){
  101. print $e->getMessage();
  102. exit();
  103. }
  104. }
  105. public function getProvince()
  106. {
  107. return $this->province;
  108. }
  109. public function getCity()
  110. {
  111. return $this->city;
  112. }
  113. public function getRegion()
  114. {
  115. return $this->region;
  116. }
  117. public function getYear()
  118. {
  119. return $this->year;
  120. }
  121. public function getMonth()
  122. {
  123. return $this->month;
  124. }
  125. public function getDay()
  126. {
  127. return $this->day;
  128. }
  129. public function getGender()
  130. {
  131. return $this->gender;
  132. }
  133. public function getBirthday()
  134. {
  135. return $this->birthday;
  136. }
  137. public function toString()
  138. {
  139. return "省份:".$this->province.",性别:".$this->gender.",出生日期:".$this->birthday;
  140. }
  141. public function getAge()
  142. {
  143. return $this->age;
  144. }
  145. }