'北京', '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; } }