我是靠谱客的博主 冷静康乃馨,这篇文章主要介绍PHP实现根据出生年月日计算年龄的功能(代码示例),现在分享给大家,希望可以做个参考。

本篇文章给大家介绍一下使用PHP实现根据出生年月日计算年龄的功能,结合实例形式分析了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
29
30
<?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'); ?>
登录后复制

运行结果:

复制代码
1
32
登录后复制

最后

以上就是冷静康乃馨最近收集整理的关于PHP实现根据出生年月日计算年龄的功能(代码示例)的全部内容,更多相关PHP实现根据出生年月日计算年龄内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(96)

评论列表共有 0 条评论

立即
投稿
返回
顶部