Home  >  Article  >  Backend Development  >  PHP function to obtain the birthday in the ID card and verify whether it is an adult

PHP function to obtain the birthday in the ID card and verify whether it is an adult

墨辰丷
墨辰丷Original
2018-06-07 10:36:392166browse

This article mainly introduces the functions of php to obtain the birthday in the ID card and verify whether it is an adult. Interested friends can refer to it. I hope it will be helpful to everyone.

php A function to extract the birthday date from the ID number and determine whether you are an adult. It can determine 15-digit and 18-digit ID cards at the same time. After personal testing, it is very easy to use. The shared function code is as follows:

<?php
//用php从身份证中提取生日,包括位和位身份证
function getIDCardInfo($IDCard){
  $result[&#39;error&#39;]=;//:未知错误,:身份证格式错误,:无错误
  $result[&#39;flag&#39;]=&#39;&#39;;//标示成年,标示未成年
  $result[&#39;tdate&#39;]=&#39;&#39;;//生日,格式如:--
  if(!eregi("^[-]([-a-zA-Z]{}|[-a-zA-Z]{})$",$IDCard)){
    $result[&#39;error&#39;]=;
    return $result;
  }else{
    if(strlen($IDCard)==){
      $tyear=intval(substr($IDCard,,));
      $tmonth=intval(substr($IDCard,,));
      $tday=intval(substr($IDCard,,));
      if($tyear>date("Y")||$tyear<(date("Y")-)){
        $flag=;
      }elseif($tmonth<||$tmonth>){
        $flag=;
      }elseif($tday<||$tday>){
        $flag=;
      }else{
        $tdate=$tyear."-".$tmonth."-".$tday." ::";
        if((time()-mktime(,,,$tmonth,$tday,$tyear))>****){
          $flag=;
        }else{
          $flag=;
        }
      }
    }elseif(strlen($IDCard)==){
      $tyear=intval("".substr($IDCard,,));
      $tmonth=intval(substr($IDCard,,));
      $tday=intval(substr($IDCard,,));
      if($tyear>date("Y")||$tyear<(date("Y")-)){
        $flag=;
      }elseif($tmonth<||$tmonth>){
        $flag=;
      }elseif($tday<||$tday>){
        $flag=;
      }else{
        $tdate=$tyear."-".$tmonth."-".$tday." ::";
        if((time()-mktime(,,,$tmonth,$tday,$tyear))>****){
          $flag=;
        }else{
          $flag=;
        }
      }
    }
  }
  $result[&#39;error&#39;]=;//:未知错误,:身份证格式错误,:无错误
  $result[&#39;isAdult&#39;]=$flag;//标示成年,标示未成年
  $result[&#39;birthday&#39;]=$tdate;//生日日期
  return $result;
}

The usage is as follows:

The code is as follows:

getIDCardInfo(&#39;身份证号码&#39;);

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Mysql Three commonly used functions

How to get a specified column in an array in PHP (Case)

How does PHP determine the operator based on the mobile phone number (Example)

The above is the detailed content of PHP function to obtain the birthday in the ID card and verify whether it is an adult. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn