开发者

PHP Accurately Calculate Nearest Age for a given DOB

开发者 https://www.devze.com 2023-03-31 11:36 出处:网络
I am trying to calculate the nearest Age based on DOB, but i cant wrap my head around how to do it. I have tried some methods which estimates but this is not good enough. We need to calculate the days

I am trying to calculate the nearest Age based on DOB, but i cant wrap my head around how to do it. I have tried some methods which estimates but this is not good enough. We need to calculate the days from today and the next birthday, whether it is in the current year or next year. and also calculate the days from today and the last birthday again whether i开发者_高级运维t is in the current year or last year.

Any suggestions?


I think this is what you want.... of course, you could just get a persons age accurate to the day and round it up or down to the closest year..... which is probably what I should have done.

It's quite brute force, so I'm sure you can do it better, but what it does is check the number of days until this year's, next year's, and last year's birthday (I checked each of those three separately instead of subtracting from 365, since date() takes care of leap years, and I don't want to). Then it calculates age from whichever one of those birthdays is closest.

Working example

<?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.";
?>

Edit: Removed unnecessary date()s in the $time_until calcs.


If I understand correctly you want to "round" the age? Then how about something along these lines:

$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');
}


Edit: rewrote to use DateInterval

This should do the trick for you...

$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;
0

精彩评论

暂无评论...
验证码 换一张
取 消