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