当前位置:网站首页>Get the relevant information of ID card through PHP, get the zodiac, get the constellation, get the age, and get the gender

Get the relevant information of ID card through PHP, get the zodiac, get the constellation, get the age, and get the gender

2022-07-06 01:45:00 Geek iron Han

Here are some Encapsulated method .


/** *  Whether the verification of ID card is legal  * * @param string $idcard  Complete body  *  Certificate No  */
function checkIdCard($idcard = '')
{
    
    //  Can only be 18 position 
    if (strlen($idcard) != 18) {
    
        return false;
    }
    //  Take out the body code 
    $idcard_base = substr($idcard, 0, 17);
    //  Take out the check code 
    $verify_code = substr($idcard, 17, 1);
    //  Weighting factors 
    $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
    //  Check code corresponding value 
    $verify_code_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
    //  According to the former 17 Bit calculation check code 
    $total = 0;
    for ($i = 0; $i < 17; $i++) {
    
        $total += substr($idcard_base, $i, 1) * $factor[$i];
    }
    //  modulus 
    $mod = $total % 11;
    //  Comparison check code 
    if ($verify_code == $verify_code_list[$mod]) {
    
        //  If the age calculation fails , Then the judgment is   The ID number is incorrect. 
        if (ageVerification($idcard)) {
    
            return true;
        }
    }
    return false;
}


/** *  Get birthday according to ID number  * inupt $code =  Complete ID number  * @return string Y-m-d */
function getBirthday($code = '')
{
    
    $age_time = strtotime(substr($code, 6, 8));
    if ($age_time === false) {
    
        return false;
    }
    return date("Y-m-d", $age_time);
}
/** *  Get the age according to the ID number  * inupt $code =  Complete ID number  * @return $age :  Age   Three figures   Such as 023 */
function ageVerification($code = '')
{
    
    $age_time = strtotime(substr($code, 6, 8));
    if ($age_time === false) {
    
        return false;
    }
    list($y1, $m1, $d1) = explode("-", date("Y-m-d", $age_time));

    $now_time = strtotime("now");

    list($y2, $m2, $d2) = explode("-", date("Y-m-d", $now_time));
    $age = $y2 - $y1;
    if ((int)($m2 . $d2) < (int)($m1 . $d1)) {
    
        $age -= 1;
    }
    return $age;
}

/** *  Obtain gender according to ID card 1 male 2 Woman  */
function getSex($cid = ''): int
{
    
    // Return to gender according to ID number 
    if (!checkIdCard($cid)) {
    
        return 0;
    }
    $sexint = (int) substr($cid, 16, 1);

    return 0 === $sexint % 2 ? 2 : 1;
}
/** *  According to identity  *  Certificate No , Automatically return to the corresponding constellation  */
function getXingZuo($cid = '')
{
    
    //  According to ID number , Automatically return to the corresponding constellation 
    if (!checkIdCard($cid)) {
    
        return 0;
    }
    $bir = substr($cid, 10, 4);
    $month = (int) substr($bir, 0, 2);
    $day = (int) substr($bir, 2);
    $strValue = '';
    if ((1 == $month && $day <= 21) || (2 == $month && $day <= 19)) {
    
        $strValue = 0; // Aquarius: 
    } elseif ((2 == $month && $day > 20) || (3 == $month && $day <= 20)) {
    
        $strValue = 1; // Pisces 
    } elseif ((3 == $month && $day > 20) || (4 == $month && $day <= 20)) {
    
        $strValue = 2; // Aries 
    } elseif ((4 == $month && $day > 20) || (5 == $month && $day <= 21)) {
    
        $strValue = 3; // Taurus 
    } elseif ((5 == $month && $day > 21) || (6 == $month && $day <= 21)) {
    
        $strValue = 4; // Gemini 
    } elseif ((6 == $month && $day > 21) || (7 == $month && $day <= 22)) {
    
        $strValue = 5; // Cancer 
    } elseif ((7 == $month && $day > 22) || (8 == $month && $day <= 23)) {
    
        $strValue = 6; // Leo 
    } elseif ((8 == $month && $day > 23) || (9 == $month && $day <= 23)) {
    
        $strValue = 7; // Virgo 
    } elseif ((9 == $month && $day > 23) || (10 == $month && $day <= 23)) {
    
        $strValue = 8; // libra 
    } elseif ((10 == $month && $day > 23) || (11 == $month && $day <= 22)) {
    
        $strValue = 9; // scorpio 
    } elseif ((11 == $month && $day > 22) || (12 == $month && $day <= 21)) {
    
        $strValue = 10; // Sagittarius 
    } elseif ((12 == $month && $day > 21) || (1 == $month && $day <= 20)) {
    
        $strValue = 11; // Capricorn 
    }

    return $strValue;
}
/** *  Return the corresponding Zodiac according to the ID number.  */
function getSh
engXiao($cid = ''): int
{
    

    if (!checkIdCard($cid)) {
    
        return 0;
    }
    $start = 1901;
    $end = $end = (int) substr($cid, 6, 4);
    $x = ($start - $end) % 12;
    $value = '';
    if (1 == $x || -11 == $x) {
    
        $value = 0; // rat 
    }
    if (0 == $x) {
    
        $value = 1; // cattle 
    }
    if (11 == $x || -1 == $x) {
    
        $value = 2; // The tiger 
    }
    if (10 == $x || -2 == $x) {
    
        $value = 3; // rabbit 
    }
    if (9 == $x || -3 == $x) {
    
        $value = 4; // dragon 
    }
    if (8 == $x || -4 == $x) {
    
        $value = 5; // The snake 
    }
    if (7 == $x || -5 == $x) {
    
        $value = 6; // Horse 
    }
    if (6 == $x || -6 == $x) {
    
        $value = 7; // sheep 
    }
    if (5 == $x || -7 == $x) {
    
        $value = 8; // Monkey 
    }
    if (4 == $x || -8 == $x) {
    
        $value = 9; // chicken 
    }
    if (3 == $x || -9 == $x) {
    
        $value = 10; // Dog 
    }
    if (2 == $x || -10 == $x) {
    
        $value = 11; // The pig 
    }

    return $value;
}



原网站

版权声明
本文为[Geek iron Han]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060142334926.html