PHP calcula con precisión la edad más cercana para un DOB determinado

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 it is in the current year or last year.

¿Alguna sugerencia?

preguntado el 28 de agosto de 11 a las 00:08

Echa un vistazo a esta respuesta y el Clase DateTime. Also, if you want to just use date() luego strtotime () es tu amigo. -

@Peter ya this is what i have looked at, i just cant figure out how to implement it. -

Yeah, it might be somewhat tedious. I gave it a shot below. -- I think it works and takes care of leap year days and everything, but it's not very elegant. -

What if the current time is exactly between the last and next birthdays? Which age should be returned? -

@PhpMyCoder - Ask the mom for the time of birth. -

3 Respuestas

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

Respondido 28 ago 11, 05:08

this doesnt work. Bday '1990-09-03' gives me "20" it should give me 21 because this person is closer to his next birthday then his last birthday. its not a simple round. specially by month, because days matter too. How can i count the days to his next birthday. and how can i count the days to his last birthday. - M. de CA

I wonder if simply adding 6 months to the birthday would be enough. - maaudet

@M. of CA It was a typo in the format. With added % it returns 21. It's only "accurate" to the month though. 6 months aren't necessarily 182.5 days, so it may be slightly off. - deceze ♦

What if last year's birthday is the closest? - Peter Ajtai

@Peter Bueno, el $diff here is the age of the person. If the person is 20 years, 5 months old, we choose "20". If he's 20 years, 7 months old, we choose "21". No need to tinker with birthdays. I have to admit this is only very lightly tested though and may well not do exactly what it's supposed to. :) - deceze ♦

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.

Ejemplo de trabajo

<?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 en el $time_until calcs.

Respondido 28 ago 11, 22:08

this outputs you are 10 years old. this is not correct, this should output 11 yours old. becuase this person is 11 years old. - M. de CA

@M. of CA - You tried it before I fixed a typo. Try it now with the example listed ( codepad.viper-7.com/fPp2lj ) - Peter Ajtai

i think this is it... wow u edited this like 8 times in the last 15 min... can how we modify this so we can supply a date instead of today. - M. de CA

@M. of CA - wherever it says time() replace with the Unix timestamp of the date you want. - also the strtotime('+1 year') and strtotime('-1 year') have to be modified to add and subtract from the inputed date. - Peter Ajtai

date() around strtotime is entirely unnecessary and just happens to work through pure coincidence. - deceze ♦

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;

Respondido 28 ago 11, 07:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.