Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP精确计算给定DOB的最近年龄_Php_Date_Date Arithmetic - Fatal编程技术网

PHP精确计算给定DOB的最近年龄

PHP精确计算给定DOB的最近年龄,php,date,date-arithmetic,Php,Date,Date Arithmetic,我正试图根据出生日期来计算最接近的年龄,但我不知道该怎么做。我尝试过一些估算的方法,但这还不够好。我们需要计算从今天到下一个生日的天数,无论是在今年还是明年。并再次计算从今天起的天数和最后一个生日,无论是在今年还是去年 有什么建议吗?如果我理解正确,你想“圆”这个年龄吗?那么,沿着这些思路做些什么呢: $dob = new DateTime($birthday); $diff = $dob->diff(new DateTime); if ($diff->format('%m') &

我正试图根据出生日期来计算最接近的年龄,但我不知道该怎么做。我尝试过一些估算的方法,但这还不够好。我们需要计算从今天到下一个生日的天数,无论是在今年还是明年。并再次计算从今天起的天数和最后一个生日,无论是在今年还是去年


有什么建议吗?

如果我理解正确,你想“圆”这个年龄吗?那么,沿着这些思路做些什么呢:

$dob = new DateTime($birthday);
$diff = $dob->diff(new DateTime);

if ($diff->format('%m') > 6) {
    echo 'Age: ' . ($diff->format('%y') + 1);
} else {
    echo 'Age: ' . $diff->format('%y');
}
编辑:重写以使用

这应该对你有好处

$birthday = new DateTime('1990-09-03');
$today = new DateTime();
$diff = $birthday->diff($today, TRUE);
$age = $diff->format('%Y');
$next_birthday = $birthday->modify('+'. $age + 1 . ' years');
$halfway_to_bday = $next_birthday->sub(DateInterval::createFromDateString('182 days 12 hours'));

if($today >= $halfway_to_bday)
{
    $age++;
}

echo $age;

我想这就是你想要的。。。。当然,你可以把一个人的年龄精确到当天,然后将其向上或向下取整到最接近的年份。。。。。这可能是我应该做的

这是一种非常残酷的力量,所以我相信你可以做得更好,但它所做的是检查今年、明年和去年生日之前的天数(我分别检查了这三天,而不是从365天中减去,因为date()处理闰年,我不想这样做)。然后,它根据这些生日中最接近的一个来计算年龄


看一看和。另外,如果你只想使用
date()
,那就是你的朋友。@Peter ya这就是我所看到的,我只是不知道如何实现它。是的,它可能有点乏味。我在下面我认为它很管用,能照顾闰年和所有的事情,但它不是很优雅。如果当前时间正好在上一个生日和下一个生日之间呢?应该返回哪个年龄?@PhpMyCoder-向母亲询问出生时间。这不起作用。“1990-09-03”这一天给了我“20”,它应该给我21,因为这个人比他上一个生日更接近他的下一个生日。这不是一个简单的回合。特别是按月份,因为天数也很重要。我怎么能数到他下一个生日呢。我怎么计算他最后一个生日的天数呢?我不知道在生日的基础上再加上6个月是否就足够了。@CA的M.这是一个格式错误。添加
%
后返回21。不过,它只能“精确”到月份。6个月不一定是182.5天,所以可能会稍微缩短。如果去年的生日是最近的呢?@Peter Well,
$diff
这里是这个人的年龄。如果患者年龄为20岁5个月,我们选择“20”。如果他20岁7个月大,我们选择“21”。没有必要修改生日。我不得不承认,这只是一个非常轻微的测试,但很可能不完全符合它的要求。:)这表明你10岁了。这是不正确的,这应该输出11个旧的。因为这个人已经11岁了。@M.of CA-你在我纠正打字错误之前就试过了。现在用下面列出的例子()试试看,我想就是这个了。。。哇,你在过去的15分钟里编辑了8次。。。我们可以如何修改它,以便提供一个日期而不是今天。@M.of CA-无论它在哪里显示
time()
替换为您想要的日期的Unix时间戳。-此外,还必须修改strotime(“+1年”)和strotime(“-1年”)以从输入的日期中进行加减。
date()
round
strotime
完全没有必要,只是碰巧通过纯粹的巧合起作用。
<?php
$bday = "September 3, 1990";
// Output is 21 on 2011-08-27 for 1990-09-03

// Check the times until this, next, and last year's bdays
$time_until = strtotime(date('M j', strtotime($bday))) - time();
$this_year = abs($time_until);

$time_until = strtotime(date('M j', strtotime($bday)).' +1 year') - time();
$next_year = abs($time_until);

$time_until = strtotime(date('M j', strtotime($bday)).' -1 year') - time();
$last_year = abs($time_until);

$years = array($this_year, $next_year, $last_year);

// Calculate age based on closest bday
if (min($years) == $this_year) {
    $age = date('Y', time()) - date('Y', strtotime($bday));
}
if (min($years) == $next_year) {
    $age = date('Y', strtotime('+1 year')) - date('Y', strtotime($bday));
}
if (min($years) == $last_year) {
    $age = date('Y', strtotime('-1 year')) - date('Y', strtotime($bday));
}

echo "You are $age years old.";
?>