用PHP计算出生日期起的年龄 [英] Calculating age from date of birth in PHP

查看:86
本文介绍了用PHP计算出生日期起的年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从使用者的出生日期算起年龄,您遇到的最精确的功能是什么.我有以下代码,并且想知道如何改进它,因为它不支持所有日期格式,并且不确定它是否也是最准确的功能(符合DateTime的要求会很好).

What's the most precise function you have come across to work out an age from the users date of birth. I have the following code and was wondering how it could be improved as it doesn't support all date formats and not sure if it's the most accurate function either (DateTime compliance would be nice).

function getAge($birthday) {
    return floor((strtotime(date('d-m-Y')) - strtotime($date))/(60*60*24*365.2421896));
}

推荐答案

$birthday = new DateTime($birthday);
$interval = $birthday->diff(new DateTime);
echo $interval->y;

应该工作

这篇关于用PHP计算出生日期起的年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆