| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2017/11/29/029
- * Time: 10:10
- */
- namespace backend\helpers;
- class IdCardInfoExtractorHelper
- {
- // 省份
- private $province;
- // 城市
- private $city;
- // 区县
- private $region;
- // 年份
- private $year;
- // 月份
- private $month;
- // 日期
- private $day;
- // 性别
- private $gender;
- // 出生日期
- private $birthday;
- // 年龄
- private $age;
- private $cityCode = [
- '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' => '国外',
- ];
- private $validator = null;
- public function __construct($idcard)
- {
- try {
- $this->validator = new IdcardValidatorHelper();
- if ($this->validator->checkIdCard($idcard)) {
- if (strlen($idcard) == 15) {
- $idcard = $this->validator->convertIdcarBy15bit($idcard);
- }
- // 获取省份
- $provinceId = substr($idcard, 0, 2);
- foreach ($this->cityCode as $key=>$item) {
- if ($provinceId == $key) {
- $this->province = $item;
- break;
- }
- }
- // 获取性别
- $id17 = substr($idcard, 16, 1);
- if ($id17 % 2 != 0) {
- $this->gender = "男";
- } else {
- $this->gender = "女";
- }
- // 获取出生日期
- $birthday = substr($idcard, 6, 8);
- $d = new \DateTime($birthday);
- $dd = $d->format('Y-m-d H:i:s');
- $this->birthday = $dd;
- $this->year = substr($dd, 0, 4);
- $this->month = substr($dd, 4, 2);
- $this->day = substr($dd, 6, 2);
- $this->age = (int)date('Y') - $this->year;
- }
- } catch (\Exception $e){
- print $e->getMessage();
- exit();
- }
- }
- public function getProvince()
- {
- return $this->province;
- }
- public function getCity()
- {
- return $this->city;
- }
- public function getRegion()
- {
- return $this->region;
- }
- public function getYear()
- {
- return $this->year;
- }
- public function getMonth()
- {
- return $this->month;
- }
- public function getDay()
- {
- return $this->day;
- }
- public function getGender()
- {
- return $this->gender;
- }
- public function getBirthday()
- {
- return $this->birthday;
- }
- public function toString()
- {
- return "省份:".$this->province.",性别:".$this->gender.",出生日期:".$this->birthday;
- }
-
- public function getAge()
- {
- return $this->age;
- }
- }
|