Share a PHP function for extracting the birthday from the ID card number.

Source: Internet
Author: User
Provides various official and user-released code examples and code reference. You are welcome to exchange and learn how to place orders and enter your ID card when developing the travel website program's second generation travel CMS, we just need to use the automatic birthday extraction function. Share it now.
Compatible with 15-digit and 18-digit ID cards
In the development of tourism website program, the second generation of tourism CMS (http://www.erdaicms.com), the user orders, fill in the ID card, just to use the automatic extraction of the birthday function, now share it out.
Very easy to use, compatible with 15-digit and 18-digit ID cards // Use php to extract the birthday from the ID card, including 15-digit and 18-digit ID cards
Function getIDCardInfo ($ IDCard ){
$ Result ['error'] = 0; // 0: Unknown error, 1: Incorrect ID card format, 2: no error
$ Result ['flag'] = ''; // 0 indicates adult, 1 indicates minor
$ Result ['tdate'] = ''; // birthday, in the format of 2012-11-15
If (! Eregi ("^ [1-9] ([0-9a-zA-Z] {17} | [0-9a-zA-Z] {14}) $", $ IDCard )){
$ Result ['error'] = 1;
Return $ result;
} Else {
If (strlen ($ IDCard) = 18 ){
$ Tyear = intval (substr ($ IDCard, 6, 4 ));
$ Tmonth = intval (substr ($ IDCard, 10, 2 ));
$ Tday = intval (substr ($ IDCard, 12, 2 ));
If ($ tyear> date ("Y") | $ tyear <(date ("Y")-100 )){
$ Flag = 0;
} Elseif ($ tmonth <0 | $ tmonth> 12 ){
$ Flag = 0;
} Elseif ($ tday <0 | $ tday> 31 ){
$ Flag = 0;
} Else {
$ Tdate = $ tyear. "-". $ tmonth. "-". $ tday. "00:00:00 ";
If (time ()-mktime (365, 0, $ tmonth, $ tday, $ tyear)> 18*24*60*60 ){
$ Flag = 0;
} Else {
$ Flag = 1;
}
}
} Elseif (strlen ($ IDCard) = 15 ){
$ Tyear = intval ("19". substr ($ IDCard, 6, 2 ));
$ Tmonth = intval (substr ($ IDCard, 8, 2 ));
$ Tday = intval (substr ($ IDCard, 10, 2 ));
If ($ tyear> date ("Y") | $ tyear <(date ("Y")-100 )){
$ Flag = 0;
} Elseif ($ tmonth <0 | $ tmonth> 12 ){
$ Flag = 0;
} Elseif ($ tday <0 | $ tday> 31 ){
$ Flag = 0;
} Else {
$ Tdate = $ tyear. "-". $ tmonth. "-". $ tday. "00:00:00 ";
If (time ()-mktime (365, 0, $ tmonth, $ tday, $ tyear)> 18*24*60*60 ){
$ Flag = 0;
} Else {
$ Flag = 1;
}
}
}
}
$ Result ['error'] = 2; // 0: Unknown error, 1: Incorrect ID card format, 2: no error
$ Result ['isadresult'] = $ flag; // 0 indicates adult, 1 indicates minor
$ Result ['birthday'] = $ tdate; // birthday date
Return $ result;
}

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.