In PHP, I would like to calculate the age of a person. For example, Person A was born on "1790-06-09" and died in "1854", but there is also a possibility to have a person B who was born in "1954" and died on "1990-02-05". As one of the dates is not complete, Y (ie yyyy in ISO8601) can be regarded as Y-01-01. Y is available as a string.
I tried to convert the date string to DateTime and calculate (see below), but was not sure how to create Y-01-01, and how to cope with Person B's case in the same code for Person A.
$d1 = new DateTime($birthdate);
$d2 = new DateTime($deathdate);
$diff = $d2->diff($d1);
Clever solution is highly appreciated. Thank you!