当前位置:网站首页>通过PHP 获取身份证相关信息 获取生肖,获取星座,获取年龄,获取性别
通过PHP 获取身份证相关信息 获取生肖,获取星座,获取年龄,获取性别
2022-07-06 01:42:00 【极客铁憨憨】
下面展示一些 封装好的方法
。
/** * 身份证校验是否合法 * * @param string $idcard 完整的身 * 份证号 */
function checkIdCard($idcard = '')
{
// 只能是18位
if (strlen($idcard) != 18) {
return false;
}
// 取出本体码
$idcard_base = substr($idcard, 0, 17);
// 取出校验码
$verify_code = substr($idcard, 17, 1);
// 加权因子
$factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
// 校验码对应值
$verify_code_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
// 根据前17位计算校验码
$total = 0;
for ($i = 0; $i < 17; $i++) {
$total += substr($idcard_base, $i, 1) * $factor[$i];
}
// 取模
$mod = $total % 11;
// 比较校验码
if ($verify_code == $verify_code_list[$mod]) {
// 如果年龄计算失败,则判断为 身份证号有误
if (ageVerification($idcard)) {
return true;
}
}
return false;
}
/** * 根据身份证号码获取生日 * inupt $code = 完整的身份证号 * @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);
}
/** * 根据身份证号码获取年龄 * inupt $code = 完整的身份证号 * @return $age : 年龄 三位数 如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;
}
/** * 更据身份证获取性别1男2女 */
function getSex($cid = ''): int
{
//根据身份证号返回性别
if (!checkIdCard($cid)) {
return 0;
}
$sexint = (int) substr($cid, 16, 1);
return 0 === $sexint % 2 ? 2 : 1;
}
/** * 根据身份 * 证号,自动返回对应的星座 */
function getXingZuo($cid = '')
{
// 根据身份证号,自动返回对应的星座
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; //水瓶座
} elseif ((2 == $month && $day > 20) || (3 == $month && $day <= 20)) {
$strValue = 1; //双鱼座
} elseif ((3 == $month && $day > 20) || (4 == $month && $day <= 20)) {
$strValue = 2; //白羊座
} elseif ((4 == $month && $day > 20) || (5 == $month && $day <= 21)) {
$strValue = 3; //金牛座
} elseif ((5 == $month && $day > 21) || (6 == $month && $day <= 21)) {
$strValue = 4; //双子座
} elseif ((6 == $month && $day > 21) || (7 == $month && $day <= 22)) {
$strValue = 5; //巨蟹座
} elseif ((7 == $month && $day > 22) || (8 == $month && $day <= 23)) {
$strValue = 6; //狮子座
} elseif ((8 == $month && $day > 23) || (9 == $month && $day <= 23)) {
$strValue = 7; //处女座
} elseif ((9 == $month && $day > 23) || (10 == $month && $day <= 23)) {
$strValue = 8; //天秤座
} elseif ((10 == $month && $day > 23) || (11 == $month && $day <= 22)) {
$strValue = 9; //天蝎座
} elseif ((11 == $month && $day > 22) || (12 == $month && $day <= 21)) {
$strValue = 10; //射手座
} elseif ((12 == $month && $day > 21) || (1 == $month && $day <= 20)) {
$strValue = 11; //魔羯座
}
return $strValue;
}
/** * 根据身份证号返回对应的生肖 */
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; //鼠
}
if (0 == $x) {
$value = 1; //牛
}
if (11 == $x || -1 == $x) {
$value = 2; //虎
}
if (10 == $x || -2 == $x) {
$value = 3; //兔
}
if (9 == $x || -3 == $x) {
$value = 4; //龙
}
if (8 == $x || -4 == $x) {
$value = 5; //蛇
}
if (7 == $x || -5 == $x) {
$value = 6; //马
}
if (6 == $x || -6 == $x) {
$value = 7; //羊
}
if (5 == $x || -7 == $x) {
$value = 8; //猴
}
if (4 == $x || -8 == $x) {
$value = 9; //鸡
}
if (3 == $x || -9 == $x) {
$value = 10; //狗
}
if (2 == $x || -10 == $x) {
$value = 11; //猪
}
return $value;
}
边栏推荐
- Superfluid_ HQ hacked analysis
- selenium 等待方式
- Comments on flowable source code (XXXV) timer activation process definition processor, process instance migration job processor
- 正则表达式:示例(1)
- How does the crystal oscillator vibrate?
- [Jiudu OJ 09] two points to find student information
- Crawler request module
- 【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库
- D22:indeterminate equation (indefinite equation, translation + problem solution)
- Basic operations of databases and tables ----- primary key constraints
猜你喜欢
Docker compose配置MySQL并实现远程连接
Alibaba canal usage details (pit draining version)_ MySQL and ES data synchronization
Docker compose configures MySQL and realizes remote connection
NLP第四范式:Prompt概述【Pre-train,Prompt(提示),Predict】【刘鹏飞】
UE4 unreal engine, editor basic application, usage skills (IV)
1. Introduction to basic functions of power query
Tensorflow customize the whole training process
[技术发展-28]:信息通信网大全、新的技术形态、信息通信行业高质量发展概览
C web page open WinForm exe
【SSRF-01】服务器端请求伪造漏洞原理及利用实例
随机推荐
【已解决】如何生成漂亮的静态文档说明页
02. Go language development environment configuration
leetcode刷题_平方数之和
selenium 元素定位(2)
[flask] official tutorial -part1: project layout, application settings, definition and database access
Alibaba canal usage details (pit draining version)_ MySQL and ES data synchronization
Leetcode sum of two numbers
leetcode-2.回文判断
Sword finger offer 12 Path in matrix
Luo Gu P1170 Bugs Bunny and Hunter
Thinking about the best practice of dynamics 365 development collaboration
Poj2315 football games
Paddle框架:PaddleNLP概述【飞桨自然语言处理开发库】
leetcode3、实现 strStr()
Basic operations of databases and tables ----- default constraints
You are using pip version 21.1.1; however, version 22.0.3 is available. You should consider upgradin
Format code_ What does formatting code mean
Electrical data | IEEE118 (including wind and solar energy)
What is weak reference? What are the weak reference data types in ES6? What are weak references in JS?
国家级非遗传承人高清旺《四大美人》皮影数字藏品惊艳亮相!