企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
~~~ /** * 生日转年龄 * @param $birthday 1990-01-01 * @return false|mixed|string */ function birthday2age($birthday) { list($year, $month, $day) = explode("-", $birthday); $year_diff = date("Y") - $year; $month_diff = date("m") - $month; $day_diff = date("d") - $day; if ($day_diff < 0 || $month_diff < 0) { $year_diff--; } return $year_diff; } ~~~ ~~~ /** * $date是时间戳 * $type为1的时候是虚岁,2的时候是周岁 */ function getAgeByBirth($date, $type = 1) { $nowYear = date("Y", time()); $nowMonth = date("m", time()); $nowDay = date("d", time()); $birthYear = date("Y", $date); $birthMonth = date("m", $date); $birthDay = date("d", $date); if ($type == 1) { $age = $nowYear - ($birthYear - 1); } else { $type == 2} { if ($nowMonth < $birthMonth) { $age = $nowYear - $birthYear - 1; } elseif ($nowMonth == $birthMonth) { if ($nowDay < $birthDay) { $age = $nowYear - $birthYear - 1; } else { $age = $nowYear - $birthYear; } } else { $age = $nowYear - $birthYear; } } return $age; } ~~~ ~~~ /** * 根据身份证号获取年龄 * @param $id * @return false|float|int|mixed|string */ function getAgeByID($id) { //过了这年的生日才算多了1周岁 if (empty($id)) { return ''; } $date = strtotime(substr($id, 6, 8)); //获得出生年月日的时间戳 $today = strtotime('today'); //获得今日的时间戳 $diff = floor(($today - $date) / 86400 / 365); //得到两个日期相差的大体年数 //strtotime加上这个年数后得到那日的时间戳后与今日的时间戳相比 $age = strtotime(substr($id, 6, 8).' +'.$diff.'years') > $today ? ($diff + 1) : $diff; return $age; } ~~~