xref: /webtrees/app/Date.php (revision f882f05da0a9f2819b41b45c490062299137542d)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21a25f0a04SGreg Roach
22a80c4362SGreg Roachuse DomainException;
23a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
244a83f5d7SGreg Roachuse Fisharebest\Webtrees\Date\AbstractCalendarDate;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\FrenchDate;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\HijriDate;
280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JalaliDate;
290e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate;
300e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JulianDate;
310e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\RomanDate;
32a25f0a04SGreg Roach
33a25f0a04SGreg Roach/**
3476692c8bSGreg Roach * A representation of GEDCOM dates and date ranges.
3576692c8bSGreg Roach *
3676692c8bSGreg Roach * Since different calendars start their days at different times, (civil
37a25f0a04SGreg Roach * midnight, solar midnight, sunset, sunrise, etc.), we convert on the basis of
38a25f0a04SGreg Roach * midday.
39a25f0a04SGreg Roach *
4076692c8bSGreg Roach * We assume that years start on the first day of the first month. Where
41a25f0a04SGreg Roach * this is not the case (e.g. England prior to 1752), we need to use modified
42a25f0a04SGreg Roach * years or the OS/NS notation "4 FEB 1750/51".
43a25f0a04SGreg Roach */
44c1010edaSGreg Roachclass Date
45c1010edaSGreg Roach{
4633c746f1SGreg Roach    // Optional qualifier, such as BEF, FROM, ABT
4733c746f1SGreg Roach    public string $qual1 = '';
48a25f0a04SGreg Roach
4933c746f1SGreg Roach    // The first (or only) date
5033c746f1SGreg Roach    private AbstractCalendarDate $date1;
51a25f0a04SGreg Roach
5233c746f1SGreg Roach    // Optional qualifier, such as TO, AND
5333c746f1SGreg Roach    public string $qual2 = '';
54a25f0a04SGreg Roach
5533c746f1SGreg Roach    // Optional second date
5633c746f1SGreg Roach    private ?AbstractCalendarDate $date2 = null;
57a25f0a04SGreg Roach
5833c746f1SGreg Roach    // Optional text, as included with an INTerpreted date
5933c746f1SGreg Roach    private string $text = '';
60a25f0a04SGreg Roach
61a25f0a04SGreg Roach    /**
62a25f0a04SGreg Roach     * Create a date, from GEDCOM data.
63a25f0a04SGreg Roach     *
64a25f0a04SGreg Roach     * @param string $date A date in GEDCOM format
65a25f0a04SGreg Roach     */
66cfe766afSGreg Roach    public function __construct(string $date)
67c1010edaSGreg Roach    {
68*f882f05dSGreg Roach        $calendar_date_factory = Registry::calendarDateFactory();
69*f882f05dSGreg Roach
70a25f0a04SGreg Roach        // Extract any explanatory text
71a25f0a04SGreg Roach        if (preg_match('/^(.*) ?[(](.*)[)]/', $date, $match)) {
72a25f0a04SGreg Roach            $date       = $match[1];
73a25f0a04SGreg Roach            $this->text = $match[2];
74a25f0a04SGreg Roach        }
75a25f0a04SGreg Roach        if (preg_match('/^(FROM|BET) (.+) (AND|TO) (.+)/', $date, $match)) {
76a25f0a04SGreg Roach            $this->qual1 = $match[1];
77*f882f05dSGreg Roach            $this->date1 = $calendar_date_factory->make($match[2]);
78a25f0a04SGreg Roach            $this->qual2 = $match[3];
79*f882f05dSGreg Roach            $this->date2 = $calendar_date_factory->make($match[4]);
803506b2e5SGreg Roach        } elseif (preg_match('/^(TO|FROM|BEF|AFT|CAL|EST|INT|ABT) (.+)/', $date, $match)) {
81a25f0a04SGreg Roach            $this->qual1 = $match[1];
82*f882f05dSGreg Roach            $this->date1 = $calendar_date_factory->make($match[2]);
83a25f0a04SGreg Roach        } else {
84*f882f05dSGreg Roach            $this->date1 = $calendar_date_factory->make($date);
85a25f0a04SGreg Roach        }
86a25f0a04SGreg Roach    }
87a25f0a04SGreg Roach
88a25f0a04SGreg Roach    /**
89a25f0a04SGreg Roach     * When we copy a date object, we need to create copies of
90a25f0a04SGreg Roach     * its child objects.
91a25f0a04SGreg Roach     */
92c1010edaSGreg Roach    public function __clone()
93c1010edaSGreg Roach    {
94a25f0a04SGreg Roach        $this->date1 = clone $this->date1;
957e96c925SGreg Roach        if ($this->date2 !== null) {
96a25f0a04SGreg Roach            $this->date2 = clone $this->date2;
97a25f0a04SGreg Roach        }
98a25f0a04SGreg Roach    }
99a25f0a04SGreg Roach
100a25f0a04SGreg Roach    /**
101a25f0a04SGreg Roach     * Convert a date to the preferred format and calendar(s) display.
102a25f0a04SGreg Roach     *
10366ecd017SGreg Roach     * @param Tree|null   $tree              Wrap the date in a link to the calendar page for the tree
104a25f0a04SGreg Roach     * @param string|null $date_format       Override the default date format
10566ecd017SGreg Roach     * @param bool        $convert_calendars Convert the date into other calendars (requires a tree)
106a25f0a04SGreg Roach     *
107a25f0a04SGreg Roach     * @return string
108a25f0a04SGreg Roach     */
10966ecd017SGreg Roach    public function display(Tree $tree = null, string $date_format = null, bool $convert_calendars = false): string
110c1010edaSGreg Roach    {
11166ecd017SGreg Roach        if ($tree instanceof Tree) {
11212dcbc32SGreg Roach            $CALENDAR_FORMAT = $tree->getPreference('CALENDAR_FORMAT');
1133cccdb42SGreg Roach        } else {
11466ecd017SGreg Roach            $CALENDAR_FORMAT = 'none';
1153cccdb42SGreg Roach        }
116a25f0a04SGreg Roach
1177c6a929fSGreg Roach        $date_format = $date_format ?? I18N::dateFormat();
118a25f0a04SGreg Roach
119a25f0a04SGreg Roach        if ($convert_calendars) {
120a25f0a04SGreg Roach            $calendar_format = explode('_and_', $CALENDAR_FORMAT);
121a25f0a04SGreg Roach        } else {
12213abd6f3SGreg Roach            $calendar_format = [];
123a25f0a04SGreg Roach        }
124a25f0a04SGreg Roach
125a25f0a04SGreg Roach        // Two dates with text before, between and after
126a25f0a04SGreg Roach        $q1 = $this->qual1;
127a25f0a04SGreg Roach        $d1 = $this->date1->format($date_format, $this->qual1);
128a25f0a04SGreg Roach        $q2 = $this->qual2;
1298f038c36SRico Sonntag        if ($this->date2 === null) {
130a25f0a04SGreg Roach            $d2 = '';
131a25f0a04SGreg Roach        } else {
132a25f0a04SGreg Roach            $d2 = $this->date2->format($date_format, $this->qual2);
133a25f0a04SGreg Roach        }
134a25f0a04SGreg Roach        // Con vert to other calendars, if requested
135a25f0a04SGreg Roach        $conv1 = '';
136a25f0a04SGreg Roach        $conv2 = '';
137a25f0a04SGreg Roach        foreach ($calendar_format as $cal_fmt) {
138e364afe4SGreg Roach            if ($cal_fmt !== 'none') {
139a25f0a04SGreg Roach                $d1conv = $this->date1->convertToCalendar($cal_fmt);
140a25f0a04SGreg Roach                if ($d1conv->inValidRange()) {
141a25f0a04SGreg Roach                    $d1tmp = $d1conv->format($date_format, $this->qual1);
142a25f0a04SGreg Roach                } else {
143a25f0a04SGreg Roach                    $d1tmp = '';
144a25f0a04SGreg Roach                }
1458f038c36SRico Sonntag                if ($this->date2 === null) {
146a25f0a04SGreg Roach                    $d2conv = null;
147a25f0a04SGreg Roach                    $d2tmp  = '';
148a25f0a04SGreg Roach                } else {
149a25f0a04SGreg Roach                    $d2conv = $this->date2->convertToCalendar($cal_fmt);
150a25f0a04SGreg Roach                    if ($d2conv->inValidRange()) {
151a25f0a04SGreg Roach                        $d2tmp = $d2conv->format($date_format, $this->qual2);
152a25f0a04SGreg Roach                    } else {
153a25f0a04SGreg Roach                        $d2tmp = '';
154a25f0a04SGreg Roach                    }
155a25f0a04SGreg Roach                }
156d4f10423SGreg Roach                // If the date is different from the unconverted date, add it to the date string.
1573cfaf201SGreg Roach                if ($d1 != $d1tmp && $d1tmp !== '') {
15866ecd017SGreg Roach                    if ($tree instanceof Tree) {
1593cfaf201SGreg Roach                        if ($CALENDAR_FORMAT !== 'none') {
1607560a51fSGreg Roach                            $conv1 .= ' <span dir="' . I18N::direction() . '">(<a href="' . e($d1conv->calendarUrl($date_format, $tree)) . '" rel="nofollow">' . $d1tmp . '</a>)</span>';
161a25f0a04SGreg Roach                        } else {
1627560a51fSGreg Roach                            $conv1 .= ' <span dir="' . I18N::direction() . '"><br><a href="' . e($d1conv->calendarUrl($date_format, $tree)) . '" rel="nofollow">' . $d1tmp . '</a></span>';
163a25f0a04SGreg Roach                        }
164a25f0a04SGreg Roach                    } else {
165c0af647eSGreg Roach                        $conv1 .= ' <span dir="' . I18N::direction() . '">(' . $d1tmp . ')</span>';
166a25f0a04SGreg Roach                    }
167a25f0a04SGreg Roach                }
1688f038c36SRico Sonntag                if ($this->date2 !== null && $d2 != $d2tmp && $d1tmp != '') {
16966ecd017SGreg Roach                    if ($tree instanceof Tree) {
1707560a51fSGreg Roach                        $conv2 .= ' <span dir="' . I18N::direction() . '">(<a href="' . e($d2conv->calendarUrl($date_format, $tree)) . '" rel="nofollow">' . $d2tmp . '</a>)</span>';
171a25f0a04SGreg Roach                    } else {
172c0af647eSGreg Roach                        $conv2 .= ' <span dir="' . I18N::direction() . '">(' . $d2tmp . ')</span>';
173a25f0a04SGreg Roach                    }
174a25f0a04SGreg Roach                }
175a25f0a04SGreg Roach            }
176a25f0a04SGreg Roach        }
177a25f0a04SGreg Roach
178a25f0a04SGreg Roach        // Add URLs, if requested
17966ecd017SGreg Roach        if ($tree instanceof Tree) {
1807560a51fSGreg Roach            $d1 = '<a href="' . e($this->date1->calendarUrl($date_format, $tree)) . '" rel="nofollow">' . $d1 . '</a>';
1814a83f5d7SGreg Roach            if ($this->date2 instanceof AbstractCalendarDate) {
1827560a51fSGreg Roach                $d2 = '<a href="' . e($this->date2->calendarUrl($date_format, $tree)) . '" rel="nofollow">' . $d2 . '</a>';
183a25f0a04SGreg Roach            }
184a25f0a04SGreg Roach        }
185a25f0a04SGreg Roach
186a25f0a04SGreg Roach        // Localise the date
187a25f0a04SGreg Roach        switch ($q1 . $q2) {
188a25f0a04SGreg Roach            case '':
189a25f0a04SGreg Roach                $tmp = $d1 . $conv1;
190a25f0a04SGreg Roach                break;
191a25f0a04SGreg Roach            case 'ABT':
192bbb76c12SGreg Roach                /* I18N: Gedcom ABT dates */
193bbb76c12SGreg Roach                $tmp = I18N::translate('about %s', $d1 . $conv1);
194a25f0a04SGreg Roach                break;
195a25f0a04SGreg Roach            case 'CAL':
196bbb76c12SGreg Roach                /* I18N: Gedcom CAL dates */
197bbb76c12SGreg Roach                $tmp = I18N::translate('calculated %s', $d1 . $conv1);
198a25f0a04SGreg Roach                break;
199a25f0a04SGreg Roach            case 'EST':
200bbb76c12SGreg Roach                /* I18N: Gedcom EST dates */
201bbb76c12SGreg Roach                $tmp = I18N::translate('estimated %s', $d1 . $conv1);
202a25f0a04SGreg Roach                break;
203a25f0a04SGreg Roach            case 'INT':
204bbb76c12SGreg Roach                /* I18N: Gedcom INT dates */
205bbb76c12SGreg Roach                $tmp = I18N::translate('interpreted %s (%s)', $d1 . $conv1, e($this->text));
206a25f0a04SGreg Roach                break;
207a25f0a04SGreg Roach            case 'BEF':
208bbb76c12SGreg Roach                /* I18N: Gedcom BEF dates */
209bbb76c12SGreg Roach                $tmp = I18N::translate('before %s', $d1 . $conv1);
210a25f0a04SGreg Roach                break;
211a25f0a04SGreg Roach            case 'AFT':
212bbb76c12SGreg Roach                /* I18N: Gedcom AFT dates */
213bbb76c12SGreg Roach                $tmp = I18N::translate('after %s', $d1 . $conv1);
214a25f0a04SGreg Roach                break;
215a25f0a04SGreg Roach            case 'FROM':
216bbb76c12SGreg Roach                /* I18N: Gedcom FROM dates */
217bbb76c12SGreg Roach                $tmp = I18N::translate('from %s', $d1 . $conv1);
218a25f0a04SGreg Roach                break;
219a25f0a04SGreg Roach            case 'TO':
220bbb76c12SGreg Roach                /* I18N: Gedcom TO dates */
221bbb76c12SGreg Roach                $tmp = I18N::translate('to %s', $d1 . $conv1);
222a25f0a04SGreg Roach                break;
223a25f0a04SGreg Roach            case 'BETAND':
224bbb76c12SGreg Roach                /* I18N: Gedcom BET-AND dates */
225bbb76c12SGreg Roach                $tmp = I18N::translate('between %s and %s', $d1 . $conv1, $d2 . $conv2);
226a25f0a04SGreg Roach                break;
227a25f0a04SGreg Roach            case 'FROMTO':
228bbb76c12SGreg Roach                /* I18N: Gedcom FROM-TO dates */
229bbb76c12SGreg Roach                $tmp = I18N::translate('from %s to %s', $d1 . $conv1, $d2 . $conv2);
230a25f0a04SGreg Roach                break;
231a25f0a04SGreg Roach            default:
232a25f0a04SGreg Roach                $tmp = I18N::translate('Invalid date');
233bbb76c12SGreg Roach                break;
234a25f0a04SGreg Roach        }
235a25f0a04SGreg Roach
2365fec6c0aSGreg Roach        if (strip_tags($tmp) === '') {
2375fec6c0aSGreg Roach            return '';
238a25f0a04SGreg Roach        }
239b2ce94c6SRico Sonntag
240b2ce94c6SRico Sonntag        return '<span class="date">' . $tmp . '</span>';
241a25f0a04SGreg Roach    }
242a25f0a04SGreg Roach
243a25f0a04SGreg Roach    /**
244a25f0a04SGreg Roach     * Get the earliest calendar date from this GEDCOM date.
245a25f0a04SGreg Roach     *
246a25f0a04SGreg Roach     * In the date “FROM 1900 TO 1910”, this would be 1900.
247a25f0a04SGreg Roach     *
2484a83f5d7SGreg Roach     * @return AbstractCalendarDate
249a25f0a04SGreg Roach     */
2504a83f5d7SGreg Roach    public function minimumDate(): AbstractCalendarDate
251c1010edaSGreg Roach    {
252a25f0a04SGreg Roach        return $this->date1;
253a25f0a04SGreg Roach    }
254a25f0a04SGreg Roach
255a25f0a04SGreg Roach    /**
256a25f0a04SGreg Roach     * Get the latest calendar date from this GEDCOM date.
257a25f0a04SGreg Roach     *
258a25f0a04SGreg Roach     * In the date “FROM 1900 TO 1910”, this would be 1910.
259a25f0a04SGreg Roach     *
2604a83f5d7SGreg Roach     * @return AbstractCalendarDate
261a25f0a04SGreg Roach     */
262e364afe4SGreg Roach    public function maximumDate(): AbstractCalendarDate
263c1010edaSGreg Roach    {
264af2489e5SGreg Roach        return $this->date2 ?? $this->date1;
265a25f0a04SGreg Roach    }
266a25f0a04SGreg Roach
267a25f0a04SGreg Roach    /**
268a25f0a04SGreg Roach     * Get the earliest Julian day number from this GEDCOM date.
269a25f0a04SGreg Roach     *
270cbc1590aSGreg Roach     * @return int
271a25f0a04SGreg Roach     */
2728f53f488SRico Sonntag    public function minimumJulianDay(): int
273c1010edaSGreg Roach    {
274af2489e5SGreg Roach        return $this->minimumDate()->minimumJulianDay();
275a25f0a04SGreg Roach    }
276a25f0a04SGreg Roach
277a25f0a04SGreg Roach    /**
278a25f0a04SGreg Roach     * Get the latest Julian day number from this GEDCOM date.
279a25f0a04SGreg Roach     *
280cbc1590aSGreg Roach     * @return int
281a25f0a04SGreg Roach     */
2828f53f488SRico Sonntag    public function maximumJulianDay(): int
283c1010edaSGreg Roach    {
284af2489e5SGreg Roach        return $this->maximumDate()->maximumJulianDay();
285a25f0a04SGreg Roach    }
286a25f0a04SGreg Roach
287a25f0a04SGreg Roach    /**
288a25f0a04SGreg Roach     * Get the middle Julian day number from the GEDCOM date.
289a25f0a04SGreg Roach     *
290f5b60decSGreg Roach     * For a month-only date, this would be somewhere around the 16th day.
291a25f0a04SGreg Roach     * For a year-only date, this would be somewhere around 1st July.
292a25f0a04SGreg Roach     *
293cbc1590aSGreg Roach     * @return int
294a25f0a04SGreg Roach     */
2958f53f488SRico Sonntag    public function julianDay(): int
296c1010edaSGreg Roach    {
297cdaafeeeSGreg Roach        return intdiv($this->minimumJulianDay() + $this->maximumJulianDay(), 2);
298a25f0a04SGreg Roach    }
299a25f0a04SGreg Roach
300a25f0a04SGreg Roach    /**
301a25f0a04SGreg Roach     * Offset this date by N years, and round to the whole year.
302a25f0a04SGreg Roach     *
303a25f0a04SGreg Roach     * This is typically used to create an estimated death date,
304a25f0a04SGreg Roach     * which is before a certain number of years after the birth date.
305a25f0a04SGreg Roach     *
306cbc1590aSGreg Roach     * @param int    $years     a number of years, positive or negative
307cbc1590aSGreg Roach     * @param string $qualifier typically “BEF” or “AFT”
308a25f0a04SGreg Roach     *
309a25f0a04SGreg Roach     * @return Date
310a25f0a04SGreg Roach     */
3118f53f488SRico Sonntag    public function addYears(int $years, string $qualifier = ''): Date
312c1010edaSGreg Roach    {
313a25f0a04SGreg Roach        $tmp               = clone $this;
3144a83f5d7SGreg Roach        $tmp->date1->year  += $years;
3154a83f5d7SGreg Roach        $tmp->date1->month = 0;
3164a83f5d7SGreg Roach        $tmp->date1->day   = 0;
317a25f0a04SGreg Roach        $tmp->date1->setJdFromYmd();
318a25f0a04SGreg Roach        $tmp->qual1 = $qualifier;
319a25f0a04SGreg Roach        $tmp->qual2 = '';
320a25f0a04SGreg Roach        $tmp->date2 = null;
321a25f0a04SGreg Roach
322a25f0a04SGreg Roach        return $tmp;
323a25f0a04SGreg Roach    }
324a25f0a04SGreg Roach
325a25f0a04SGreg Roach    /**
326a25f0a04SGreg Roach     * Compare two dates, so they can be sorted.
327a25f0a04SGreg Roach     *
328054771e9SGreg Roach     * return -1 if $a<$b
329054771e9SGreg Roach     * return +1 if $b>$a
330a25f0a04SGreg Roach     * return  0 if dates same/overlap
331a25f0a04SGreg Roach     * BEF/AFT sort as the day before/after
332a25f0a04SGreg Roach     *
333a25f0a04SGreg Roach     * @param Date $a
334a25f0a04SGreg Roach     * @param Date $b
335a25f0a04SGreg Roach     *
336cbc1590aSGreg Roach     * @return int
337a25f0a04SGreg Roach     */
338e364afe4SGreg Roach    public static function compare(Date $a, Date $b): int
339c1010edaSGreg Roach    {
340a25f0a04SGreg Roach        // Get min/max JD for each date.
341a25f0a04SGreg Roach        switch ($a->qual1) {
342a25f0a04SGreg Roach            case 'BEF':
343f5b60decSGreg Roach                $amin = $a->minimumJulianDay() - 1;
344a25f0a04SGreg Roach                $amax = $amin;
345a25f0a04SGreg Roach                break;
346a25f0a04SGreg Roach            case 'AFT':
347f5b60decSGreg Roach                $amax = $a->maximumJulianDay() + 1;
348a25f0a04SGreg Roach                $amin = $amax;
349a25f0a04SGreg Roach                break;
350a25f0a04SGreg Roach            default:
351f5b60decSGreg Roach                $amin = $a->minimumJulianDay();
352f5b60decSGreg Roach                $amax = $a->maximumJulianDay();
353a25f0a04SGreg Roach                break;
354a25f0a04SGreg Roach        }
355a25f0a04SGreg Roach        switch ($b->qual1) {
356a25f0a04SGreg Roach            case 'BEF':
357f5b60decSGreg Roach                $bmin = $b->minimumJulianDay() - 1;
358a25f0a04SGreg Roach                $bmax = $bmin;
359a25f0a04SGreg Roach                break;
360a25f0a04SGreg Roach            case 'AFT':
361f5b60decSGreg Roach                $bmax = $b->maximumJulianDay() + 1;
362a25f0a04SGreg Roach                $bmin = $bmax;
363a25f0a04SGreg Roach                break;
364a25f0a04SGreg Roach            default:
365f5b60decSGreg Roach                $bmin = $b->minimumJulianDay();
366f5b60decSGreg Roach                $bmax = $b->maximumJulianDay();
367a25f0a04SGreg Roach                break;
368a25f0a04SGreg Roach        }
369a25f0a04SGreg Roach        if ($amax < $bmin) {
370a25f0a04SGreg Roach            return -1;
371a25f0a04SGreg Roach        }
372b2ce94c6SRico Sonntag
373b2ce94c6SRico Sonntag        if ($amin > $bmax && $bmax > 0) {
374b2ce94c6SRico Sonntag            return 1;
375b2ce94c6SRico Sonntag        }
376b2ce94c6SRico Sonntag
377b2ce94c6SRico Sonntag        if ($amin < $bmin && $amax <= $bmax) {
378b2ce94c6SRico Sonntag            return -1;
379b2ce94c6SRico Sonntag        }
380b2ce94c6SRico Sonntag
381b2ce94c6SRico Sonntag        if ($amin > $bmin && $amax >= $bmax && $bmax > 0) {
382b2ce94c6SRico Sonntag            return 1;
383b2ce94c6SRico Sonntag        }
384b2ce94c6SRico Sonntag
385b2ce94c6SRico Sonntag        return 0;
386a25f0a04SGreg Roach    }
387a25f0a04SGreg Roach
388a25f0a04SGreg Roach    /**
389a25f0a04SGreg Roach     * Check whether a gedcom date contains usable calendar date(s).
390a25f0a04SGreg Roach     *
391a25f0a04SGreg Roach     * An incomplete date such as "12 AUG" would be invalid, as
392a25f0a04SGreg Roach     * we cannot sort it.
393a25f0a04SGreg Roach     *
394cbc1590aSGreg Roach     * @return bool
395a25f0a04SGreg Roach     */
3968f53f488SRico Sonntag    public function isOK(): bool
397c1010edaSGreg Roach    {
398f5b60decSGreg Roach        return $this->minimumJulianDay() && $this->maximumJulianDay();
399a25f0a04SGreg Roach    }
400a25f0a04SGreg Roach
401a25f0a04SGreg Roach    /**
402a25f0a04SGreg Roach     * Calculate the gregorian year for a date. This should NOT be used internally
403a25f0a04SGreg Roach     * within WT - we should keep the code "calendar neutral" to allow support for
404a25f0a04SGreg Roach     * jewish/arabic users. This is only for interfacing with external entities,
405a25f0a04SGreg Roach     * such as the ancestry.com search interface or the dated fact icons.
406a25f0a04SGreg Roach     *
407cbc1590aSGreg Roach     * @return int
408a25f0a04SGreg Roach     */
409e364afe4SGreg Roach    public function gregorianYear(): int
410c1010edaSGreg Roach    {
411a25f0a04SGreg Roach        if ($this->isOK()) {
41259f2f229SGreg Roach            $gregorian_calendar = new GregorianCalendar();
41365e02381SGreg Roach            [$year] = $gregorian_calendar->jdToYmd($this->julianDay());
414a25f0a04SGreg Roach
415a25f0a04SGreg Roach            return $year;
416a25f0a04SGreg Roach        }
417b2ce94c6SRico Sonntag
418b2ce94c6SRico Sonntag        return 0;
419a25f0a04SGreg Roach    }
420a25f0a04SGreg Roach}
421