[person bloc] handle null values in date + improve layout on dead person

and age
This commit is contained in:
2021-08-23 19:15:00 +02:00
parent bfed062910
commit 7d6def733c
3 changed files with 31 additions and 10 deletions

View File

@@ -737,10 +737,21 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI
return $this->birthdate;
}
public function getAge(): ?int
/**
* Return the age of a person, calculated at the date 'now'.
*
* If the person has a deathdate, calculate the age at the deathdate.
*
* @param string $at a valid string to create a DateTime
* @return int|null
*/
public function getAge($at = 'now'): ?int
{
if ($this->birthdate instanceof \DateTimeInterface) {
return date_diff($this->birthdate, date_create('now'))->format("%y");
if ($this->deathdate instanceof \DateTimeInterface) {
return date_diff($this->birthdate, $this->deathdate)->format("%y");
}
return date_diff($this->birthdate, date_create($at))->format("%y");
}
return null;