当前位置:网站首页>通过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;
}
边栏推荐
- Electrical data | IEEE118 (including wind and solar energy)
- Leetcode sum of two numbers
- XSS learning XSS lab problem solution
- Superfluid_ HQ hacked analysis
- selenium 等待方式
- 02. Go language development environment configuration
- 阿里测开面试题
- Redis-字符串类型
- How does Huawei enable debug and how to make an image port
- Leetcode skimming questions_ Sum of squares
猜你喜欢

Cookie concept, basic use, principle, details and Chinese transmission

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

XSS learning XSS lab problem solution
![[detailed] several ways to quickly realize object mapping](/img/e5/70c7f8fee4556d14f969fe33938971.gif)
[detailed] several ways to quickly realize object mapping

MUX VLAN configuration

3D vision - 4 Getting started with gesture recognition - using mediapipe includes single frame and real time video

Basic operations of databases and tables ----- unique constraints

A Cooperative Approach to Particle Swarm Optimization
![[solved] how to generate a beautiful static document description page](/img/c1/6ad935c1906208d81facb16390448e.png)
[solved] how to generate a beautiful static document description page

National intangible cultural heritage inheritor HD Wang's shadow digital collection of "Four Beauties" made an amazing debut!
随机推荐
Redis string type
【Flask】静态文件与模板渲染
Huawei converged VLAN principle and configuration
leetcode-2. Palindrome judgment
D22:indeterminate equation (indefinite equation, translation + problem solution)
Alibaba canal usage details (pit draining version)_ MySQL and ES data synchronization
Code review concerns
leetcode-2.回文判断
Basic operations of database and table ----- delete data table
Alibaba-Canal使用详解(排坑版)_MySQL与ES数据同步
dried food! Accelerating sparse neural network through hardware and software co design
Basic process and testing idea of interface automation
【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库
Open source | Ctrip ticket BDD UI testing framework flybirds
ClickOnce does not support request execution level 'requireAdministrator'
Folio.ink 免费、快速、易用的图片分享工具
PHP error what is an error?
Basic operations of databases and tables ----- non empty constraints
【全网最全】 |MySQL EXPLAIN 完全解读
A Cooperative Approach to Particle Swarm Optimization