当前位置:网站首页>通过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;
}
边栏推荐
- Mongodb problem set
- How to upgrade kubernetes in place
- [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.
- 正则表达式:示例(1)
- Bidding promotion process
- MCU lightweight system core
- Leetcode skimming questions_ Verify palindrome string II
- [flask] response, session and message flashing
- Crawler request module
- [Jiudu OJ 09] two points to find student information
猜你喜欢
Une image! Pourquoi l'école t'a - t - elle appris à coder, mais pourquoi pas...
插卡4G工业路由器充电桩智能柜专网视频监控4G转以太网转WiFi有线网速测试 软硬件定制
[Jiudu OJ 09] two points to find student information
Leetcode sum of two numbers
Idea sets the default line break for global newly created files
2022年PMP项目管理考试敏捷知识点(8)
NLP fourth paradigm: overview of prompt [pre train, prompt, predict] [Liu Pengfei]
国家级非遗传承人高清旺《四大美人》皮影数字藏品惊艳亮相!
Basic operations of databases and tables ----- default constraints
Win10 add file extension
随机推荐
[the most complete in the whole network] |mysql explain full interpretation
剑指 Offer 12. 矩阵中的路径
【网络攻防实训习题】
Install redis
Numpy array index slice
How to get the PHP version- How to get the PHP Version?
Paddle framework: paddlenlp overview [propeller natural language processing development library]
2022 PMP project management examination agile knowledge points (8)
【已解决】如何生成漂亮的静态文档说明页
竞价推广流程
500 lines of code to understand the principle of mecached cache client driver
SPIR-V初窥
ClickOnce does not support request execution level 'requireAdministrator'
Extracting key information from TrueType font files
【全網最全】 |MySQL EXPLAIN 完全解讀
Folio. Ink is a free, fast and easy-to-use image sharing tool
Shutter doctor: Xcode installation is incomplete
【Flask】官方教程(Tutorial)-part3:blog蓝图、项目可安装化
Redis-列表
Initialize MySQL database when docker container starts