php通过出生日期计算出年龄,实现办法,首先将出生日期的年月日转化为时间戳,然后通过当前时间戳减去出生年月日的时间戳,从而计算出年龄。php通过出生日期计算出年龄,实现办法,首先将出生日期的年月日转化为时间戳,然后通过当前时间戳减去出生年月日的时间戳,从而计算出年龄。 详细代码如下: /** * 准备工作完毕 开始计算年龄函数 * @param $birthday 出生时间 uninx时间戳 * @par

详细代码如下:
/**
* 准备工作完毕 开始计算年龄函数
* @param $birthday 出生时间 uninx时间戳
* @param $time 当前时间
**/
function getAge($birthday){
//格式化出生时间年月日
$byear=date('Y',$birthday);
$bmonth=date('m',$birthday);
$bday=date('d',$birthday);
//格式化当前时间年月日
$tyear=date('Y');
$tmonth=date('m');
$tday=date('d');
//开始计算年龄
$age=$tyear-$byear;
if($bmonth>$tmonth || $bmonth==$tmonth && $bday>$tday){
$age--;
}
return $age;
}
$riqi='1989-10-18 15:20:36';
$uriqi=strtotime($riqi); //将日期转化为时间戳
$age=getAge($uriqi);
echo '<br><br>年龄计算结果:'.$age.'岁';
沃梦达教程
本文标题为:PHP根据出生日期计算年龄
基础教程推荐
猜你喜欢
- php怎么判断一个数组中的元素是否完全属于另一个数组 2024-12-04
- 基于PHP+MySQL的简单聊天室思路与代码 2022-08-27
- php利用函数将二进制转为字符串的两种方法 2024-12-04
- PHP实现根据数组的值进行分组 2022-08-01
- php利用for语句和substr()函数怎么实现字符串的反转 2024-12-04
- php如何利用函数修改时差 2024-12-05
- php利用函数获取两个数组中不同元素的个数 2024-12-04
- 前端用PHP写简单的删除接口(三) 2024-12-04
- php基于文本搜索的实例代码 2022-08-22
- 掌握php六大关键字 2024-12-04
