1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees; 21 22use function view; 23 24/** 25 * The difference between two GEDCOM dates. 26 */ 27class Age 28{ 29 /** @var int */ 30 private $years; 31 32 /** @var int */ 33 private $months; 34 35 /** @var int */ 36 private $days; 37 38 /** @var int */ 39 private $total_days; 40 41 /** @var bool */ 42 private $is_exact; 43 44 /** @var bool */ 45 private $is_valid; 46 47 /** 48 * Age constructor. 49 * 50 * @param Date $x - The first date 51 * @param Date $y - The second date 52 */ 53 public function __construct(Date $x, Date $y) 54 { 55 // If the dates are ranges, use the start/end calendar dates. 56 $start = $x->minimumDate(); 57 $end = $y->maximumDate(); 58 59 [$this->years, $this->months, $this->days] = $start->ageDifference($end); 60 61 $this->total_days = $end->minimumJulianDay() - $start->minimumJulianDay(); 62 63 // Use the same precision as found in the dates. 64 if ($start->day() === 0 || $end->day() === 0) { 65 $this->days = 0; 66 } 67 68 if ($start->month() === 0 || $end->month() === 0) { 69 $this->months = 0; 70 } 71 72 // Are the dates exact? 73 $this->is_exact = $start->day() !== 0 && $end->day() !== 0; 74 75 // Are the dates valid? 76 $this->is_valid = $x->isOK() && $y->isOK(); 77 } 78 79 /** 80 * Show an age in a human-friendly form, such as "34 years", "8 months", "20 days". 81 * Show an empty string for invalid/missing dates. 82 * Show a warning icon for negative ages. 83 * Show zero ages without any units. 84 * 85 * @return string 86 */ 87 public function __toString(): string 88 { 89 if (!$this->is_valid) { 90 return ''; 91 } 92 93 if ($this->years < 0) { 94 return view('icons/warning'); 95 } 96 97 if ($this->years > 0) { 98 return I18N::plural('%s year', '%s years', $this->years, I18N::number($this->years)); 99 } 100 101 if ($this->months > 0) { 102 return I18N::plural('%s month', '%s months', $this->months, I18N::number($this->months)); 103 } 104 105 if ($this->days > 0 || $this->is_exact) { 106 return I18N::plural('%s day', '%s days', $this->days, I18N::number($this->days)); 107 } 108 109 return I18N::number(0); 110 } 111 112 /** 113 * How many days between two events? 114 * If either date is invalid return -1. 115 * 116 * @return int 117 */ 118 public function ageDays(): int 119 { 120 if ($this->is_valid) { 121 return $this->total_days; 122 } 123 124 return -1; 125 } 126 127 /** 128 * How many years between two events? 129 * Return -1 for invalid or reversed dates. 130 * 131 * @return int 132 */ 133 public function ageYears(): int 134 { 135 if ($this->is_valid) { 136 return $this->years; 137 } 138 139 return -1; 140 } 141 142 /** 143 * How many years between two events? 144 * If either date is invalid return -1. 145 * 146 * @return string 147 */ 148 public function ageYearsString(): string 149 { 150 if (!$this->is_valid) { 151 return ''; 152 } 153 154 if ($this->years < 0) { 155 return view('icons/warning'); 156 } 157 158 159 return I18N::number($this->years); 160 } 161} 162