PHP常用函数之根据生日计算年龄功能示例
这篇文章主要介绍了PHP常用函数之根据生日计算年龄功能,结合实例形式分析了php日期相关转换与计算操作技巧,需要的朋友可以参考下
/**
* 根据出生年月日计算出年龄
* @param $birth_year
* @param $birth_month
* @param $birth_day
* @return int
*/
function getAgeByBirth($birth_year,$birth_month,$birth_day){
if(empty($birth_year) || empty($birth_month) || empty($birth_day)){
return 0;
}
$current_year = date('Y',time());
$current_month = date('m',time());
$current_day = date('d',time());
if($birth_year >= $current_year){
return 0;
}
$age = $current_year - $birth_year - 1;
if($current_month>$birth_month){
return $age+1;
}else if($current_month == $birth_month && $current_day>=$birth_day){
return $age+1;
}else{
return $age;
}
}
//测试:
echo getAgeByBirth('1988','8','8');
运行结果:
31
微信扫描下方的二维码阅读本文
阅读剩余
版权声明:
标题:PHP常用函数之根据生日计算年龄功能示例
作者:admin
链接:https://www.kmtky.com/532.html
文章版权归作者所有,未经允许请勿转载。
声明:仅供内部测试和技术交流使用,任何非法商业使用及商业利益冲突带来的法律纠纷,与本人无关,本人概不负责,请下载后24小时内删除,谢谢合作!如有侵权请于作者联系,尽快删除。
标题:PHP常用函数之根据生日计算年龄功能示例
作者:admin
链接:https://www.kmtky.com/532.html
文章版权归作者所有,未经允许请勿转载。
声明:仅供内部测试和技术交流使用,任何非法商业使用及商业利益冲突带来的法律纠纷,与本人无关,本人概不负责,请下载后24小时内删除,谢谢合作!如有侵权请于作者联系,尽快删除。
THE END