PHP常用函数之根据生日计算年龄功能示例

吾爱主题 阅读:107 2021-09-09 16:14:00 评论:0

本文实例讲述了PHP常用函数之根据生日计算年龄功能。分享给大家供大家参考,具体如下:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 /**   * 根据出生年月日计算出年龄   * @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程序设计有所帮助。

原文链接:https://www.cnblogs.com/gyfluck/p/8521197.html

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容