PHP实现根据出生年月日计算年龄的功能(代码示例)-php教程

资源魔 42 0
本篇文章给各人引见一下应用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','4','8');
?>

运转后果:

32

更多相干常识,请存眷 PHP中文网!!

以上就是PHP完成依据出身年代日较量争论春秋的性能(代码示例)的具体内容,更多请存眷资源魔其它相干文章!

标签: php php开发教程 php开发资料 php开发自学

抱歉,评论功能暂时关闭!