Where am I going wrong to calculate age?

Asked

Viewed 118 times

2

function idadeAluno($dataNasci){
    $nasci = new dateTime($this->dataNasci);
    $agora = new dateTime();
    $idade = $this->nasci - $this->agora;
    echo 'idade aluno:'.$this->idade.'<br/>';
}
  • You can not reproduce the error, because there are a number of problems in the post (for example, where $this comes from?) Important to post a [mcve] that presents the problem occurred.

3 answers

3

To calculate the difference between two DateTime's, just use the method diff:

$date1 = new DateTime("2000-01-20");
$date2 = new DateTime("2019-04-23");
$interval = $date1->diff($date2);
echo "diferença " . $interval->y . " anos, " . $interval->m." meses, ".$interval->d." dias"; 

The result is a DateInterval, which has the values of the difference properly "broken" in years, months and days. In this case the output is:

difference 19 years, 3 months, 3 days

If you only want the amount of years, no matter the months or days, just take the value of $interval->y.

Just remembering that the amount of years is always rounded down:

$date1 = new DateTime("2018-01-20"); // 20/jan/2018
$date2 = new DateTime("2019-01-19"); // 19/jan/2019
$interval = $date1->diff($date2);
echo "diferença " . $interval->y;

In the above example, the value of $interval->y is zero. Even if it takes one day to complete a year, and technically the age is 0.999... years, the value is rounded down and the result is zero. Only January 20, 2019 will complete 1 year.


There are some edge cases to consider. For example, if someone was born on February 29, 2000, on February 28, 2001 that person will already have 1 year?

$date1 = new DateTime("2000-02-29");
$date2 = new DateTime("2001-02-28");
$interval = $date1->diff($date2);
echo "diferença " . $interval->y . " anos, " . $interval->m." meses, ".$interval->d." dias"; 

PHP understands that it does not:

difference 0 years, 11 months, 30 days

In "real life", I really don’t know what the rule should be (some Apis in other languages consider that in the above case the difference is 1 year, for example), so it’s up to you to analyze these special cases and decide what the result should be.

  • Works perfectly with tiny d -> https://ideone.com/AV3Bin

  • @Bacco Dessa I didn’t know. It’s "by design"?

  • 2

    PHP things :D - Almost everything is insensitive to PHP case, but as it is PHP, "depends on the context" kkkkk - Qq way, the question has a lot of problems that can cause the most various errors, besides the lack of diff

1

Learn about diff here. This is useful to represent the difference between the two dates.

Your code would look like this:

  function idadeAluno($dataNasci)
    {
    $nasci = new DateTime($dataNasci);
    $agora = new DateTime();
    $idade = $nasci->diff($agora);

    return "idade aluno: ".$idade->y. "<br/>"; 
     }

    echo idadeAluno("2007-03-24");

1

Hitching a ride on @hkotsubo’s reply, DateTime is misspelled, plus a few syntax errors, for example when using $this->dataNasci to refer to the parameter it is sufficient $dataNasci as it is a parameter and not a property.

Here is corrected code:

class CalculaData{
    function idadeAluno($dataNasci)
    {
        $nasci = new DateTime($dataNasci);
        $agora = new DateTime("now");

        $idade = $nasci->diff($agora);
         echo "idade aluno: " . $idade->y . " anos, " . $idade->m." meses, ".$idade->d." dias";
    }
}

$calculaData = new CalculaData();

$calculaData->idadeAluno("1935-06-01");

Whose return is :

idade aluno: 83 anos, 10 meses, 13 dias   
  • 1

    thanks for showing the errors, it’s tip worked .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.