当前位置:网站首页>通过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;
}
边栏推荐
- 02. Go language development environment configuration
- 2022 PMP project management examination agile knowledge points (8)
- 阿裏測開面試題
- MUX VLAN configuration
- leetcode-两数之和
- Sword finger offer 38 Arrangement of strings
- What is weak reference? What are the weak reference data types in ES6? What are weak references in JS?
- module ‘tensorflow. contrib. data‘ has no attribute ‘dataset
- 干货!通过软硬件协同设计加速稀疏神经网络
- [flask] official tutorial -part2: Blueprint - view, template, static file
猜你喜欢
TrueType字体文件提取关键信息
Basic operations of databases and tables ----- default constraints
【详细】快速实现对象映射的几种方式
[flask] official tutorial -part3: blog blueprint, project installability
Basic operations of databases and tables ----- primary key constraints
Electrical data | IEEE118 (including wind and solar energy)
Win10 add file extension
[technology development -28]: overview of information and communication network, new technology forms, high-quality development of information and communication industry
Leetcode skimming questions_ Invert vowels in a string
Basic process and testing idea of interface automation
随机推荐
Numpy array index slice
Unity VR resource flash surface in scene
XSS learning XSS lab problem solution
Accelerating spark data access with alluxio in kubernetes
Redis key operation
【网络攻防实训习题】
leetcode刷题_反转字符串中的元音字母
SPIR-V初窺
【已解决】如何生成漂亮的静态文档说明页
Electrical data | IEEE118 (including wind and solar energy)
Open source | Ctrip ticket BDD UI testing framework flybirds
1. Introduction to basic functions of power query
NLP fourth paradigm: overview of prompt [pre train, prompt, predict] [Liu Pengfei]
阿里测开面试题
[technology development -28]: overview of information and communication network, new technology forms, high-quality development of information and communication industry
Basic process and testing idea of interface automation
How to upgrade kubernetes in place
Bidding promotion process
2022 Guangxi Autonomous Region secondary vocational group "Cyberspace Security" competition and its analysis (super detailed)
leetcode刷题_验证回文字符串 Ⅱ