当前位置:网站首页>通过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;
}
边栏推荐
- Redis-字符串类型
- 安装Redis
- Code Review关注点
- [detailed] several ways to quickly realize object mapping
- NLP第四范式:Prompt概述【Pre-train,Prompt(提示),Predict】【刘鹏飞】
- Une image! Pourquoi l'école t'a - t - elle appris à coder, mais pourquoi pas...
- XSS learning XSS lab problem solution
- Mongodb problem set
- Yii console method call, Yii console scheduled task
- 2 power view
猜你喜欢

晶振是如何起振的?

Ordinary people end up in Global trade, and a new round of structural opportunities emerge

leetcode刷题_反转字符串中的元音字母

Numpy array index slice

You are using pip version 21.1.1; however, version 22.0.3 is available. You should consider upgradin

A picture to understand! Why did the school teach you coding but still not

c#网页打开winform exe

Card 4G industrial router charging pile intelligent cabinet private network video monitoring 4G to Ethernet to WiFi wired network speed test software and hardware customization

一图看懂!为什么学校教了你Coding但还是不会的原因...

【SSRF-01】服务器端请求伪造漏洞原理及利用实例
随机推荐
[Jiudu OJ 09] two points to find student information
【Flask】响应、session与Message Flashing
【Flask】官方教程(Tutorial)-part2:蓝图-视图、模板、静态文件
[ssrf-01] principle and utilization examples of server-side Request Forgery vulnerability
Basic operations of database and table ----- set the fields of the table to be automatically added
Alibaba canal usage details (pit draining version)_ MySQL and ES data synchronization
Open source | Ctrip ticket BDD UI testing framework flybirds
Redis-列表
Unity VR solves the problem that the handle ray keeps flashing after touching the button of the UI
Selenium element positioning (2)
[understanding of opportunity-39]: Guiguzi - Chapter 5 flying clamp - warning 2: there are six types of praise. Be careful to enjoy praise as fish enjoy bait.
Redis daemon cannot stop the solution
Huawei Hrbrid interface and VLAN division based on IP
一图看懂!为什么学校教了你Coding但还是不会的原因...
安装Redis
[flask] static file and template rendering
阿裏測開面試題
【SSRF-01】服务器端请求伪造漏洞原理及利用实例
[solved] how to generate a beautiful static document description page
Redis-Key的操作