PHP身份证校验基类

本文介绍了一个PHP类,用于验证中国身份证号码的有效性,并从中提取关键信息如生日和性别等。该类支持15位及18位身份证号码的验证,并确保了日期格式的准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php
/**
 * 身份证相关操作函数
 * Created by 乐杨俊
 */

namespace leyangjun\Lib;

class IdCard
{
    public static function validate($id_number)
    {
        $id_number = strtoupper($id_number);
        $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
        $arr_split = array();
        if(!preg_match($regx, $id_number)) {
            return false;
        }
        //检查15位
        if(15==strlen($id_number)) {
            $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";
            preg_match($regx, $id_number, $arr_split);
            //检查生日日期是否正确
            $dtm_birth = "19".$arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
            return (bool) strtotime($dtm_birth);
        } else {
            //检查18位
            $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
            preg_match($regx, $id_number, $arr_split);
            $dtm_birth = $arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
            //检查生日日期是否正确
            if(!strtotime($dtm_birth)) {
                return false;
            } else {
                //检验18位身份证的校验码是否正确。
                //校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。
                $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
                $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
                $sign = 0;
                for ( $i = 0; $i < 17; $i++ ) {
                    $b = (int) $id_number{$i};
                    $w = $arr_int[$i];
                    $sign += $b * $w;
                }
                $n  = $sign % 11;
                return $arr_ch[$n] == substr($id_number,17, 1);
            }
        }
    }

    // 获取生日
    public static function getBirthDay($id_number)
    {
        $birth_year = substr($id_number, 6,4);
        $birth_month = substr($id_number, 10,2);
        $birth_day = substr($id_number, 12,2);

        return "{$birth_year}-{$birth_month}-{$birth_day}";
    }

    // 判断是否已经成年
    public static function isAdult($id_number)
    {
        $birth_year = (int) substr($id_number, 6,4);
        $birth_month = (int) substr($id_number, 10,2);
        $birth_day = (int) substr($id_number, 12,2);
        $target_time = strtotime(($birth_year + 18).'-'.$birth_month.'-'.$birth_day);

        // 过完生日后的零时为成年人
        // 02-29出生的孩子,03-01是成年,因此strtotime会直接变为03-01,其他的日期都需要过一天
        $target_time += ($birth_month == 02 && $birth_day == 29) ? 0 : 86400;
        return $this->getCurrentTime() > $target_time;
    }


    //根据身份证号获取性别
    function getSex($cardNum) {
        return ((int)substr($cardNum, 16, 1)) % 2 === 0 ? '女' : '男';
    }

    
    public function getCurrentTime()
    {
        if (php_sapi_name() == 'cli') {
            return time();
        }
        if (!$currentTime) {
            if (isset($_SERVER['REQUEST_TIME'])) {
                $currentTime = $_SERVER['REQUEST_TIME'];
            } else {
                $currentTime = time();
            }
        }
        return $currentTime;
    }


}
简介:class IdcardAction extends Action{   function get_xingzuo($cid) {      if (!$this->isIdCard($cid)) return ';     $bir = substr($cid,10,4);     $month = (int)substr($bir,0,2);     $day = (int)substr($bir,2);     $strValue = ';     if (($month == 1 && $day >= 20) || ($month == 2 && $day <= 18)) {       $strValue = "水瓶座";     } else if (($month == 2 && $day >= 19) || ($month == 3 && $day <= 20)) {       $strValue = "双鱼座";     } else if (($month == 3 && $day > 20) || ($month == 4 && $day <= 19)) {       $strValue = "白羊座";     } else if (($month == 4 && $day >= 20) || ($month == 5 && $day <= 20)) {       $strValue = "金牛座";     } else if (($month == 5 && $day >= 21) || ($month == 6 && $day <= 21)) {       $strValue = "双子座";     } else if (($month == 6 && $day > 21) || ($month == 7 && $day <= 22)) {       $strValue = "巨蟹座";     } else if (($month == 7 && $day > 22) || ($month == 8 && $day <= 22)) {       $strValue = "狮子座";     } else if (($month == 8 && $day >= 23) || ($month == 9 && $day <= 22)) {       $strValue = "处女座";     } else if (($month == 9 && $day >= 23) || ($month == 10 && $day <= 23)) {       $strValue = "天秤座";     } else if (($month == 10 && $day > 23) || ($month == 11 && $day <= 22)) {       $strValue = "天蝎座";     } else if (($month == 11 && $day > 22) || ($month == 12 && $day <= 21)) {       $strValue = "射手座";     } else if (($month == 12 && $day > 21) || ($month == 1 && $day <= 19)) {       $strValue = "魔羯座";     }     return $strValue;   } PHP根据身份证号,自动获取对应的星座函数,然后自动返回对应的星座,自动返回性别,判断是否成年
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值