您的位置:首页 > 编程语言 > PHP开发

利用php语言提取身份证中生日号码

2018-03-15 16:51 459 查看
4000

//用php从身份证中提取生日,包括15位和18位身份证
02
function
 
getIDCardInfo(
$IDCard
,
$format
=1){
03
    
$result
[
'error'
]=0;
//0:未知错误,1:身份证格式错误,2:无错误
04
    
$result
[
'flag'
]=
''
;
//0标示成年,1标示未成年
05
    
$result
[
'tdate'
]=
''
;
//生日,格式如:2012-11-15
06
    
if
(!preg_match(
"/^(\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$/"
,
$IDCard
)){
07
        
$result
[
'error'
]=1;
08
        
return
 
$result
;
09
    
}
else
{
10
        
if
(
strlen
(
$IDCard
)==18)
11
        
{
12
            
$tyear
=
intval
(
substr
(
$IDCard
,6,4));
13
            
$tmonth
=
intval
(
substr
(
$IDCard
,10,2));
14
            
$tday
=
intval
(
substr
(
$IDCard
,12,2));
15
        
}
16
        
elseif
(
strlen
(
$IDCard
)==15)
17
        
{
18
            
$tyear
=
intval
(
"19"
.
substr
(
$IDCard
,6,2));
19
            
$tmonth
=
intval
(
substr
(
$IDCard
,8,2));
20
            
$tday
=
intval
(
substr
(
$IDCard
,10,2));
21
        
}
22
          
 
23
        
if
(
$tyear
>
date
(
"Y"
)||
$tyear
<(
date
(
"Y"
)-100))
24
        
{
25
                
$flag
=0;
26
            
}
27
            
elseif
(
$tmonth
<0||
$tmonth
>12)
28
            
{
29
                
$flag
=0;
30
            
}
31
            
elseif
(
$tday
<0||
$tday
>31)
32
            
{
33
                
$flag
=0;
34
            
}
else
35
            
{
36
                
if
(
$format
)
37
                
{
38
                    
$tdate
=
$tyear
.
"-"
.
$tmonth
.
"-"
.
$tday
;
39
                
}
40
                
else
41
                
{
42
                    
$tdate
=
$tmonth
.
"-"
.
$tday
;
43
                
}
44
                  
 
45
                
if
((time()-
mktime
(0,0,0,
$tmonth
,
$tday
,
$tyear
))>18*365*24*60*60)
46
                
{
47
                    
$flag
=0;
48
                
}
49
                
else
50
 
4000
               
{
51
                    
$flag
=1;
52
                
}
53
            
}      
54
    
}
55
    
$result
[
'error'
]=2;
//0:未知错误,1:身份证格式错误,2:无错误
56
    
$result
[
'isAdult'
]=
$flag
;
//0标示成年,1标示未成年
57
    
$result
[
'birthday'
]=
$tdate
;
//生日日期
58
    
return
 
$result
;
59
}
文章来源:北大青鸟学校开发小组
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: