IdentityCard.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. declare (strict_types=1);
  3. namespace library\utils;
  4. // +----------------------------------------------------------------------
  5. // | [ WE CAN DO IT MORE SIMPLE ]
  6. // +----------------------------------------------------------------------
  7. // | Copyright (c) 2018-2020 rights reserved.
  8. // +----------------------------------------------------------------------
  9. // | Author: TABLE ME
  10. // +----------------------------------------------------------------------
  11. // | Date: 2021/3/16 上午9:01
  12. // +----------------------------------------------------------------------
  13. class IdentityCard
  14. {
  15. /**
  16. * 校验身份证号是否合法
  17. * @param string $num 待校验的身份证号
  18. * @return bool
  19. */
  20. public static function isValid(string $num)
  21. {
  22. //老身份证长度15位,新身份证长度18位
  23. $length = strlen($num);
  24. if ($length == 15) { //如果是15位身份证
  25. //15位身份证没有字母
  26. if (!is_numeric($num)) {
  27. return false;
  28. }
  29. // 省市县(6位)
  30. $areaNum = substr($num, 0, 6);
  31. // 出生年月(6位)
  32. $dateNum = substr($num, 6, 6);
  33. } else if ($length == 18) { //如果是18位身份证
  34. //基本格式校验
  35. if (!preg_match('/^\d{17}[0-9xX]$/', $num)) {
  36. return false;
  37. }
  38. // 省市县(6位)
  39. $areaNum = substr($num, 0, 6);
  40. // 出生年月日(8位)
  41. $dateNum = substr($num, 6, 8);
  42. } else { //假身份证
  43. return false;
  44. }
  45. //验证地区
  46. if (!self::isAreaCodeValid($areaNum)) {
  47. return false;
  48. }
  49. //验证日期
  50. if (!self::isDateValid($dateNum)) {
  51. return false;
  52. }
  53. //验证最后一位
  54. if (!self::isVerifyCodeValid($num)) {
  55. return false;
  56. }
  57. return true;
  58. }
  59. /**
  60. * 省市自治区校验
  61. * @param string $area 省、直辖市代码
  62. * @return bool
  63. */
  64. private static function isAreaCodeValid(string $area)
  65. {
  66. $provinceCode = substr($area, 0, 2);
  67. // 根据GB/T2260—999,省市代码11到65
  68. if (11 <= $provinceCode && $provinceCode <= 65) {
  69. return true;
  70. } else {
  71. return false;
  72. }
  73. }
  74. /**
  75. * 验证出生日期合法性
  76. * @param string $date 日期
  77. * @return bool
  78. */
  79. private static function isDateValid(string $date)
  80. {
  81. if (strlen($date) == 6) { //15位身份证号没有年份,这里拼上年份
  82. $date = '19' . $date;
  83. }
  84. $year = intval(substr($date, 0, 4));
  85. $month = intval(substr($date, 4, 2));
  86. $day = intval(substr($date, 6, 2));
  87. //日期基本格式校验
  88. if (!checkdate($month, $day, $year)) {
  89. return false;
  90. }
  91. //日期格式正确,但是逻辑存在问题(如:年份大于当前年)
  92. $currYear = date('Y');
  93. if ($year > $currYear) {
  94. return false;
  95. }
  96. return true;
  97. }
  98. /**
  99. * 验证18位身份证最后一位
  100. * @param string $num 待校验的身份证号
  101. * @return bool
  102. */
  103. private static function isVerifyCodeValid(string $num)
  104. {
  105. if (strlen($num) == 18) {
  106. $factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  107. $tokens = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
  108. $checkSum = 0;
  109. for ($i = 0; $i < 17; $i++) {
  110. $checkSum += intval($num[$i]) * $factor[$i];
  111. }
  112. $mod = $checkSum % 11;
  113. $token = $tokens[$mod];
  114. $lastChar = strtoupper($num[17]);
  115. if ($lastChar != $token) {
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. /**
  122. * 根据身份证号码获取性别
  123. * author:xiaochuan
  124. * @param string $idcard 身份证号码
  125. * @return int $sex 性别 1男 2女 0未知
  126. */
  127. public static function get_sex($idcard) {
  128. if(empty($idcard)) return null;
  129. $sexint = (int) substr($idcard, 16, 1);
  130. return $sexint % 2 === 0 ? '女' : '男';
  131. }
  132. /**
  133. * 根据身份证号码获取生日
  134. * author:xiaochuan
  135. * @param string $idcard 身份证号码
  136. * @return $birthday
  137. */
  138. public static function get_birthday($idcard) {
  139. if(empty($idcard)) return null;
  140. $bir = substr($idcard, 6, 8);
  141. $year = (int) substr($bir, 0, 4);
  142. $month = (int) substr($bir, 4, 2);
  143. $day = (int) substr($bir, 6, 2);
  144. return $year . "-" . $month . "-" . $day;
  145. }
  146. /**
  147. * 根据身份证号码计算年龄
  148. * author:xiaochuan
  149. * @param string $idcard 身份证号码
  150. * @return int $age
  151. */
  152. public static function get_age($idcard){
  153. if(empty($idcard)) return null;
  154. # 获得出生年月日的时间戳
  155. $date = strtotime(substr($idcard,6,8));
  156. # 获得今日的时间戳
  157. $today = strtotime('today');
  158. # 得到两个日期相差的大体年数
  159. $diff = floor(($today-$date)/86400/365);
  160. # strtotime加上这个年数后得到那日的时间戳后与今日的时间戳相比
  161. $age = strtotime(substr($idcard,6,8).' +'.$diff.'years')>$today?($diff+1):$diff;
  162. return $age;
  163. }
  164. /**
  165. * 根据身份证号码获取出身地址
  166. * author:xiaochuan
  167. * @param string $idcard 身份证号码
  168. * @return string $address
  169. */
  170. public static function get_address($idcard, $type=1){
  171. if(empty($idcard)) return null;
  172. $address = include('./address.php');
  173. switch ($type) {
  174. case 1:
  175. # 截取前六位数(获取基体到县区的地址)
  176. $key = substr($idcard,0,6);
  177. if(!empty($address[$key])) return $address[$key];
  178. # 截取前两位数(没有基体到县区的地址就获取省份)
  179. $key = substr($idcard,0,2);
  180. if(!empty($address[$key])) return $address[$key];
  181. # 都没有
  182. return '未知地址';
  183. break;
  184. case 2:
  185. # 截取前两位数(只获取省份)
  186. $key = substr($idcard,0,2);
  187. if(!empty($address[$key])) return $address[$key];
  188. break;
  189. default:
  190. return null;
  191. break;
  192. }
  193. }
  194. /**
  195. * 根据身份证号,返回对应的生肖
  196. * author:xiaochuan
  197. * @param string $idcard 身份证号码
  198. */
  199. public static function get_zodiac($idcard){ //
  200. if(empty($idcard)) return null;
  201. $start = 1901;
  202. $end = (int)substr($idcard, 6, 4);
  203. $x = ($start - $end) % 12;
  204. $val = '';
  205. if ($x == 1 || $x == -11) $val = '鼠';
  206. if ($x == 0) $val = '牛';
  207. if ($x == 11 || $x == -1) $val = '虎';
  208. if ($x == 10 || $x == -2) $val = '兔';
  209. if ($x == 9 || $x == -3) $val = '龙';
  210. if ($x == 8 || $x == -4) $val = '蛇';
  211. if ($x == 7 || $x == -5) $val = '马';
  212. if ($x == 6 || $x == -6) $val = '羊';
  213. if ($x == 5 || $x == -7) $val = '猴';
  214. if ($x == 4 || $x == -8) $val = '鸡';
  215. if ($x == 3 || $x == -9) $val = '狗';
  216. if ($x == 2 || $x == -10) $val = '猪';
  217. return $val;
  218. }
  219. /**
  220. * 根据身份证号,返回对应的星座
  221. * author:xiaochuan
  222. * @param string $idcard 身份证号码
  223. */
  224. public static function get_starsign($idcard){
  225. if(empty($idcard)) return null;
  226. $b = substr($idcard, 10, 4);
  227. $m = (int)substr($b, 0, 2);
  228. $d = (int)substr($b, 2);
  229. $val = '';
  230. if(($m == 1 && $d <= 21) || ($m == 2 && $d <= 19)){
  231. $val = "水瓶座";
  232. }else if (($m == 2 && $d > 20) || ($m == 3 && $d <= 20)){
  233. $val = "双鱼座";
  234. }else if (($m == 3 && $d > 20) || ($m == 4 && $d <= 20)){
  235. $val = "白羊座";
  236. }else if (($m == 4 && $d > 20) || ($m == 5 && $d <= 21)){
  237. $val = "金牛座";
  238. }else if (($m == 5 && $d > 21) || ($m == 6 && $d <= 21)){
  239. $val = "双子座";
  240. }else if (($m == 6 && $d > 21) || ($m == 7 && $d <= 22)){
  241. $val = "巨蟹座";
  242. }else if (($m == 7 && $d > 22) || ($m == 8 && $d <= 23)){
  243. $val = "狮子座";
  244. }else if (($m == 8 && $d > 23) || ($m == 9 && $d <= 23)){
  245. $val = "处女座";
  246. }else if (($m == 9 && $d > 23) || ($m == 10 && $d <= 23)){
  247. $val = "天秤座";
  248. }else if (($m == 10 && $d > 23) || ($m == 11 && $d <= 22)){
  249. $val = "天蝎座";
  250. }else if (($m == 11 && $d > 22) || ($m == 12 && $d <= 21)){
  251. $val = "射手座";
  252. }else if (($m == 12 && $d > 21) || ($m == 1 && $d <= 20)){
  253. $val = "魔羯座";
  254. }
  255. return $val;
  256. }
  257. }