1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 5*89f7189bSGreg 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 15*89f7189bSGreg 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 333cccdb42SGreg Roachuse function app; 34054771e9SGreg Roachuse function trigger_error; 35054771e9SGreg Roach 36054771e9SGreg Roachuse const E_USER_DEPRECATED; 373cccdb42SGreg Roach 38a25f0a04SGreg Roach/** 3976692c8bSGreg Roach * A representation of GEDCOM dates and date ranges. 4076692c8bSGreg Roach * 4176692c8bSGreg Roach * Since different calendars start their days at different times, (civil 42a25f0a04SGreg Roach * midnight, solar midnight, sunset, sunrise, etc.), we convert on the basis of 43a25f0a04SGreg Roach * midday. 44a25f0a04SGreg Roach * 4576692c8bSGreg Roach * We assume that years start on the first day of the first month. Where 46a25f0a04SGreg Roach * this is not the case (e.g. England prior to 1752), we need to use modified 47a25f0a04SGreg Roach * years or the OS/NS notation "4 FEB 1750/51". 48a25f0a04SGreg Roach */ 49c1010edaSGreg Roachclass Date 50c1010edaSGreg Roach{ 51a25f0a04SGreg Roach /** @var string Optional qualifier, such as BEF, FROM, ABT */ 52fe11e66dSGreg Roach public $qual1 = ''; 53a25f0a04SGreg Roach 544a83f5d7SGreg Roach /** @var AbstractCalendarDate The first (or only) date */ 55f5b60decSGreg Roach private $date1; 56a25f0a04SGreg Roach 57a25f0a04SGreg Roach /** @var string Optional qualifier, such as TO, AND */ 58fe11e66dSGreg Roach public $qual2 = ''; 59a25f0a04SGreg Roach 604a83f5d7SGreg Roach /** @var AbstractCalendarDate|null Optional second date */ 61d0810bd3SGreg Roach private $date2; 62a25f0a04SGreg Roach 6348599914SGreg Roach /** @var string Optional text, as included with an INTerpreted date */ 647e96c925SGreg Roach private $text = ''; 65a25f0a04SGreg Roach 66a25f0a04SGreg Roach /** 67a25f0a04SGreg Roach * Create a date, from GEDCOM data. 68a25f0a04SGreg Roach * 69a25f0a04SGreg Roach * @param string $date A date in GEDCOM format 70a25f0a04SGreg Roach */ 71cfe766afSGreg Roach public function __construct(string $date) 72c1010edaSGreg Roach { 73a25f0a04SGreg Roach // Extract any explanatory text 74a25f0a04SGreg Roach if (preg_match('/^(.*) ?[(](.*)[)]/', $date, $match)) { 75a25f0a04SGreg Roach $date = $match[1]; 76a25f0a04SGreg Roach $this->text = $match[2]; 77a25f0a04SGreg Roach } 78a25f0a04SGreg Roach if (preg_match('/^(FROM|BET) (.+) (AND|TO) (.+)/', $date, $match)) { 79a25f0a04SGreg Roach $this->qual1 = $match[1]; 80a25f0a04SGreg Roach $this->date1 = $this->parseDate($match[2]); 81a25f0a04SGreg Roach $this->qual2 = $match[3]; 82a25f0a04SGreg Roach $this->date2 = $this->parseDate($match[4]); 833506b2e5SGreg Roach } elseif (preg_match('/^(TO|FROM|BEF|AFT|CAL|EST|INT|ABT) (.+)/', $date, $match)) { 84a25f0a04SGreg Roach $this->qual1 = $match[1]; 85a25f0a04SGreg Roach $this->date1 = $this->parseDate($match[2]); 86a25f0a04SGreg Roach } else { 87a25f0a04SGreg Roach $this->date1 = $this->parseDate($date); 88a25f0a04SGreg Roach } 89a25f0a04SGreg Roach } 90a25f0a04SGreg Roach 91a25f0a04SGreg Roach /** 92a25f0a04SGreg Roach * When we copy a date object, we need to create copies of 93a25f0a04SGreg Roach * its child objects. 94a25f0a04SGreg Roach */ 95c1010edaSGreg Roach public function __clone() 96c1010edaSGreg Roach { 97a25f0a04SGreg Roach $this->date1 = clone $this->date1; 987e96c925SGreg Roach if ($this->date2 !== null) { 99a25f0a04SGreg Roach $this->date2 = clone $this->date2; 100a25f0a04SGreg Roach } 101a25f0a04SGreg Roach } 102a25f0a04SGreg Roach 103a25f0a04SGreg Roach /** 104a25f0a04SGreg Roach * Convert a calendar date, such as "12 JUN 1943" into calendar date object. 105a25f0a04SGreg Roach * A GEDCOM date range may have two calendar dates. 106a25f0a04SGreg Roach * 107a25f0a04SGreg Roach * @param string $date 108a25f0a04SGreg Roach * 109a80c4362SGreg Roach * @throws DomainException 1104a83f5d7SGreg Roach * @return AbstractCalendarDate 111a25f0a04SGreg Roach */ 1124a83f5d7SGreg Roach private function parseDate($date): AbstractCalendarDate 113c1010edaSGreg Roach { 114a25f0a04SGreg Roach // Valid calendar escape specified? - use it 115bee1f90bSGreg Roach if (preg_match('/^(@#D(?:GREGORIAN|JULIAN|HEBREW|HIJRI|JALALI|FRENCH R|ROMAN)+@) ?(.*)/', $date, $match)) { 116a25f0a04SGreg Roach $cal = $match[1]; 117a25f0a04SGreg Roach $date = $match[2]; 118a25f0a04SGreg Roach } else { 119a25f0a04SGreg Roach $cal = ''; 120a25f0a04SGreg Roach } 121a25f0a04SGreg Roach // A date with a month: DM, M, MY or DMY 122a25f0a04SGreg Roach if (preg_match('/^(\d?\d?) ?(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC|TSH|CSH|KSL|TVT|SHV|ADR|ADS|NSN|IYR|SVN|TMZ|AAV|ELL|VEND|BRUM|FRIM|NIVO|PLUV|VENT|GERM|FLOR|PRAI|MESS|THER|FRUC|COMP|MUHAR|SAFAR|RABI[AT]|JUMA[AT]|RAJAB|SHAAB|RAMAD|SHAWW|DHUAQ|DHUAH|FARVA|ORDIB|KHORD|TIR|MORDA|SHAHR|MEHR|ABAN|AZAR|DEY|BAHMA|ESFAN) ?((?:\d{1,4}(?: B\.C\.)?|\d\d\d\d\/\d\d)?)$/', $date, $match)) { 123a25f0a04SGreg Roach $d = $match[1]; 124a25f0a04SGreg Roach $m = $match[2]; 125a25f0a04SGreg Roach $y = $match[3]; 126471edd97SGreg Roach } elseif (preg_match('/^(\d{1,4}(?: B\.C\.)?|\d\d\d\d\/\d\d)$/', $date, $match)) { 127471edd97SGreg Roach // A date with just a year 128a25f0a04SGreg Roach $d = ''; 129a25f0a04SGreg Roach $m = ''; 130a25f0a04SGreg Roach $y = $match[1]; 131a25f0a04SGreg Roach } else { 132a25f0a04SGreg Roach // An invalid date - do the best we can. 133a25f0a04SGreg Roach $d = ''; 134a25f0a04SGreg Roach $m = ''; 135a25f0a04SGreg Roach $y = ''; 136a25f0a04SGreg Roach // Look for a 3/4 digit year anywhere in the date 137a25f0a04SGreg Roach if (preg_match('/\b(\d{3,4})\b/', $date, $match)) { 138a25f0a04SGreg Roach $y = $match[1]; 139a25f0a04SGreg Roach } 140a25f0a04SGreg Roach // Look for a month anywhere in the date 141a25f0a04SGreg Roach if (preg_match('/(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC|TSH|CSH|KSL|TVT|SHV|ADR|ADS|NSN|IYR|SVN|TMZ|AAV|ELL|VEND|BRUM|FRIM|NIVO|PLUV|VENT|GERM|FLOR|PRAI|MESS|THER|FRUC|COMP|MUHAR|SAFAR|RABI[AT]|JUMA[AT]|RAJAB|SHAAB|RAMAD|SHAWW|DHUAQ|DHUAH|FARVA|ORDIB|KHORD|TIR|MORDA|SHAHR|MEHR|ABAN|AZAR|DEY|BAHMA|ESFAN)/', $date, $match)) { 142a25f0a04SGreg Roach $m = $match[1]; 143a25f0a04SGreg Roach // Look for a day number anywhere in the date 144a25f0a04SGreg Roach if (preg_match('/\b(\d\d?)\b/', $date, $match)) { 145a25f0a04SGreg Roach $d = $match[1]; 146a25f0a04SGreg Roach } 147a25f0a04SGreg Roach } 148a25f0a04SGreg Roach } 149a25f0a04SGreg Roach 150a25f0a04SGreg Roach // Unambiguous dates - override calendar escape 151a25f0a04SGreg Roach if (preg_match('/^(TSH|CSH|KSL|TVT|SHV|ADR|ADS|NSN|IYR|SVN|TMZ|AAV|ELL)$/', $m)) { 1524a83f5d7SGreg Roach $cal = JewishDate::ESCAPE; 153e364afe4SGreg Roach } elseif (preg_match('/^(VEND|BRUM|FRIM|NIVO|PLUV|VENT|GERM|FLOR|PRAI|MESS|THER|FRUC|COMP)$/', $m)) { 1544a83f5d7SGreg Roach $cal = FrenchDate::ESCAPE; 155e364afe4SGreg Roach } elseif (preg_match('/^(MUHAR|SAFAR|RABI[AT]|JUMA[AT]|RAJAB|SHAAB|RAMAD|SHAWW|DHUAQ|DHUAH)$/', $m)) { 1564a83f5d7SGreg Roach $cal = HijriDate::ESCAPE; // This is a WT extension 157e364afe4SGreg Roach } elseif (preg_match('/^(FARVA|ORDIB|KHORD|TIR|MORDA|SHAHR|MEHR|ABAN|AZAR|DEY|BAHMA|ESFAN)$/', $m)) { 1584a83f5d7SGreg Roach $cal = JalaliDate::ESCAPE; // This is a WT extension 159a25f0a04SGreg Roach } elseif (preg_match('/^\d{1,4}( B\.C\.)|\d\d\d\d\/\d\d$/', $y)) { 1604a83f5d7SGreg Roach $cal = JulianDate::ESCAPE; 161a25f0a04SGreg Roach } 162a25f0a04SGreg Roach 163a25f0a04SGreg Roach // Ambiguous dates - don't override calendar escape 164e364afe4SGreg Roach if ($cal === '') { 165a25f0a04SGreg Roach if (preg_match('/^(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)$/', $m)) { 1664a83f5d7SGreg Roach $cal = GregorianDate::ESCAPE; 167e364afe4SGreg Roach } elseif (preg_match('/^[345]\d\d\d$/', $y)) { 168a25f0a04SGreg Roach // Year 3000-5999 1694a83f5d7SGreg Roach $cal = JewishDate::ESCAPE; 170a25f0a04SGreg Roach } else { 1714a83f5d7SGreg Roach $cal = GregorianDate::ESCAPE; 172a25f0a04SGreg Roach } 173a25f0a04SGreg Roach } 174a25f0a04SGreg Roach // Now construct an object of the correct type 175a25f0a04SGreg Roach switch ($cal) { 1764a83f5d7SGreg Roach case GregorianDate::ESCAPE: 177c1010edaSGreg Roach return new GregorianDate([ 178c1010edaSGreg Roach $y, 179c1010edaSGreg Roach $m, 180c1010edaSGreg Roach $d, 181c1010edaSGreg Roach ]); 1824a83f5d7SGreg Roach case JulianDate::ESCAPE: 183c1010edaSGreg Roach return new JulianDate([ 184c1010edaSGreg Roach $y, 185c1010edaSGreg Roach $m, 186c1010edaSGreg Roach $d, 187c1010edaSGreg Roach ]); 1884a83f5d7SGreg Roach case JewishDate::ESCAPE: 189c1010edaSGreg Roach return new JewishDate([ 190c1010edaSGreg Roach $y, 191c1010edaSGreg Roach $m, 192c1010edaSGreg Roach $d, 193c1010edaSGreg Roach ]); 1944a83f5d7SGreg Roach case HijriDate::ESCAPE: 195c1010edaSGreg Roach return new HijriDate([ 196c1010edaSGreg Roach $y, 197c1010edaSGreg Roach $m, 198c1010edaSGreg Roach $d, 199c1010edaSGreg Roach ]); 2004a83f5d7SGreg Roach case FrenchDate::ESCAPE: 201c1010edaSGreg Roach return new FrenchDate([ 202c1010edaSGreg Roach $y, 203c1010edaSGreg Roach $m, 204c1010edaSGreg Roach $d, 205c1010edaSGreg Roach ]); 2064a83f5d7SGreg Roach case JalaliDate::ESCAPE: 207c1010edaSGreg Roach return new JalaliDate([ 208c1010edaSGreg Roach $y, 209c1010edaSGreg Roach $m, 210c1010edaSGreg Roach $d, 211c1010edaSGreg Roach ]); 2124a83f5d7SGreg Roach case RomanDate::ESCAPE: 213c1010edaSGreg Roach return new RomanDate([ 214c1010edaSGreg Roach $y, 215c1010edaSGreg Roach $m, 216c1010edaSGreg Roach $d, 217c1010edaSGreg Roach ]); 218a25f0a04SGreg Roach default: 219a80c4362SGreg Roach throw new DomainException('Invalid calendar'); 220a25f0a04SGreg Roach } 221a25f0a04SGreg Roach } 222a25f0a04SGreg Roach 2234e6225d5SGreg Roach /** 2244e6225d5SGreg Roach * A list of supported calendars and their names. 2254e6225d5SGreg Roach * 2264e6225d5SGreg Roach * @return string[] 2274e6225d5SGreg Roach */ 2288f53f488SRico Sonntag public static function calendarNames(): array 229c1010edaSGreg Roach { 23013abd6f3SGreg Roach return [ 231bbb76c12SGreg Roach /* I18N: The gregorian calendar */ 232bbb76c12SGreg Roach 'gregorian' => I18N::translate('Gregorian'), 233bbb76c12SGreg Roach /* I18N: The julian calendar */ 234bbb76c12SGreg Roach 'julian' => I18N::translate('Julian'), 235bbb76c12SGreg Roach /* I18N: The French calendar */ 236bbb76c12SGreg Roach 'french' => I18N::translate('French'), 237bbb76c12SGreg Roach /* I18N: The Hebrew/Jewish calendar */ 238bbb76c12SGreg Roach 'jewish' => I18N::translate('Jewish'), 239bbb76c12SGreg Roach /* I18N: The Arabic/Hijri calendar */ 240bbb76c12SGreg Roach 'hijri' => I18N::translate('Hijri'), 241bbb76c12SGreg Roach /* I18N: The Persian/Jalali calendar */ 242bbb76c12SGreg Roach 'jalali' => I18N::translate('Jalali'), 24313abd6f3SGreg Roach ]; 2444e6225d5SGreg Roach } 2454e6225d5SGreg Roach 246a25f0a04SGreg Roach /** 247a25f0a04SGreg Roach * Convert a date to the preferred format and calendar(s) display. 248a25f0a04SGreg Roach * 249cbc1590aSGreg Roach * @param bool|null $url Wrap the date in a link to calendar.php 250a25f0a04SGreg Roach * @param string|null $date_format Override the default date format 251cbc1590aSGreg Roach * @param bool|null $convert_calendars Convert the date into other calendars 252a25f0a04SGreg Roach * 253a25f0a04SGreg Roach * @return string 254a25f0a04SGreg Roach */ 255e364afe4SGreg Roach public function display($url = false, $date_format = null, $convert_calendars = true): string 256c1010edaSGreg Roach { 2573a5741c7SGreg Roach // Do we need a new DateFormatterService class? 2583cccdb42SGreg Roach if (app()->has(Tree::class)) { 259cab242e7SGreg Roach $tree = app(Tree::class); 26012dcbc32SGreg Roach $CALENDAR_FORMAT = $tree->getPreference('CALENDAR_FORMAT'); 2613cccdb42SGreg Roach } else { 2623cccdb42SGreg Roach $tree = null; 2633cccdb42SGreg Roach $CALENDAR_FORMAT = ''; 2643cccdb42SGreg Roach } 265a25f0a04SGreg Roach 266a25f0a04SGreg Roach if ($date_format === null) { 267dfeee0a8SGreg Roach $date_format = I18N::dateFormat(); 268a25f0a04SGreg Roach } 269a25f0a04SGreg Roach 270a25f0a04SGreg Roach if ($convert_calendars) { 271a25f0a04SGreg Roach $calendar_format = explode('_and_', $CALENDAR_FORMAT); 272a25f0a04SGreg Roach } else { 27313abd6f3SGreg Roach $calendar_format = []; 274a25f0a04SGreg Roach } 275a25f0a04SGreg Roach 276a25f0a04SGreg Roach // Two dates with text before, between and after 277a25f0a04SGreg Roach $q1 = $this->qual1; 278a25f0a04SGreg Roach $d1 = $this->date1->format($date_format, $this->qual1); 279a25f0a04SGreg Roach $q2 = $this->qual2; 2808f038c36SRico Sonntag if ($this->date2 === null) { 281a25f0a04SGreg Roach $d2 = ''; 282a25f0a04SGreg Roach } else { 283a25f0a04SGreg Roach $d2 = $this->date2->format($date_format, $this->qual2); 284a25f0a04SGreg Roach } 285a25f0a04SGreg Roach // Con vert to other calendars, if requested 286a25f0a04SGreg Roach $conv1 = ''; 287a25f0a04SGreg Roach $conv2 = ''; 288a25f0a04SGreg Roach foreach ($calendar_format as $cal_fmt) { 289e364afe4SGreg Roach if ($cal_fmt !== 'none') { 290a25f0a04SGreg Roach $d1conv = $this->date1->convertToCalendar($cal_fmt); 291a25f0a04SGreg Roach if ($d1conv->inValidRange()) { 292a25f0a04SGreg Roach $d1tmp = $d1conv->format($date_format, $this->qual1); 293a25f0a04SGreg Roach } else { 294a25f0a04SGreg Roach $d1tmp = ''; 295a25f0a04SGreg Roach } 2968f038c36SRico Sonntag if ($this->date2 === null) { 297a25f0a04SGreg Roach $d2conv = null; 298a25f0a04SGreg Roach $d2tmp = ''; 299a25f0a04SGreg Roach } else { 300a25f0a04SGreg Roach $d2conv = $this->date2->convertToCalendar($cal_fmt); 301a25f0a04SGreg Roach if ($d2conv->inValidRange()) { 302a25f0a04SGreg Roach $d2tmp = $d2conv->format($date_format, $this->qual2); 303a25f0a04SGreg Roach } else { 304a25f0a04SGreg Roach $d2tmp = ''; 305a25f0a04SGreg Roach } 306a25f0a04SGreg Roach } 307d4f10423SGreg Roach // If the date is different from the unconverted date, add it to the date string. 3083cfaf201SGreg Roach if ($d1 != $d1tmp && $d1tmp !== '') { 309a25f0a04SGreg Roach if ($url) { 3103cfaf201SGreg Roach if ($CALENDAR_FORMAT !== 'none') { 31149d5f1d7SGreg Roach $conv1 .= ' <span dir="' . I18N::direction() . '">(<a href="' . $d1conv->calendarUrl($date_format, $tree) . '" rel="nofollow">' . $d1tmp . '</a>)</span>'; 312a25f0a04SGreg Roach } else { 31349d5f1d7SGreg Roach $conv1 .= ' <span dir="' . I18N::direction() . '"><br><a href="' . $d1conv->calendarUrl($date_format, $tree) . '" rel="nofollow">' . $d1tmp . '</a></span>'; 314a25f0a04SGreg Roach } 315a25f0a04SGreg Roach } else { 316c0af647eSGreg Roach $conv1 .= ' <span dir="' . I18N::direction() . '">(' . $d1tmp . ')</span>'; 317a25f0a04SGreg Roach } 318a25f0a04SGreg Roach } 3198f038c36SRico Sonntag if ($this->date2 !== null && $d2 != $d2tmp && $d1tmp != '') { 320a25f0a04SGreg Roach if ($url) { 32149d5f1d7SGreg Roach $conv2 .= ' <span dir="' . I18N::direction() . '">(<a href="' . $d2conv->calendarUrl($date_format, $tree) . '" rel="nofollow">' . $d2tmp . '</a>)</span>'; 322a25f0a04SGreg Roach } else { 323c0af647eSGreg Roach $conv2 .= ' <span dir="' . I18N::direction() . '">(' . $d2tmp . ')</span>'; 324a25f0a04SGreg Roach } 325a25f0a04SGreg Roach } 326a25f0a04SGreg Roach } 327a25f0a04SGreg Roach } 328a25f0a04SGreg Roach 329a25f0a04SGreg Roach // Add URLs, if requested 330a25f0a04SGreg Roach if ($url) { 33149d5f1d7SGreg Roach $d1 = '<a href="' . $this->date1->calendarUrl($date_format, $tree) . '" rel="nofollow">' . $d1 . '</a>'; 3324a83f5d7SGreg Roach if ($this->date2 instanceof AbstractCalendarDate) { 33349d5f1d7SGreg Roach $d2 = '<a href="' . $this->date2->calendarUrl($date_format, $tree) . '" rel="nofollow">' . $d2 . '</a>'; 334a25f0a04SGreg Roach } 335a25f0a04SGreg Roach } 336a25f0a04SGreg Roach 337a25f0a04SGreg Roach // Localise the date 338a25f0a04SGreg Roach switch ($q1 . $q2) { 339a25f0a04SGreg Roach case '': 340a25f0a04SGreg Roach $tmp = $d1 . $conv1; 341a25f0a04SGreg Roach break; 342a25f0a04SGreg Roach case 'ABT': 343bbb76c12SGreg Roach /* I18N: Gedcom ABT dates */ 344bbb76c12SGreg Roach $tmp = I18N::translate('about %s', $d1 . $conv1); 345a25f0a04SGreg Roach break; 346a25f0a04SGreg Roach case 'CAL': 347bbb76c12SGreg Roach /* I18N: Gedcom CAL dates */ 348bbb76c12SGreg Roach $tmp = I18N::translate('calculated %s', $d1 . $conv1); 349a25f0a04SGreg Roach break; 350a25f0a04SGreg Roach case 'EST': 351bbb76c12SGreg Roach /* I18N: Gedcom EST dates */ 352bbb76c12SGreg Roach $tmp = I18N::translate('estimated %s', $d1 . $conv1); 353a25f0a04SGreg Roach break; 354a25f0a04SGreg Roach case 'INT': 355bbb76c12SGreg Roach /* I18N: Gedcom INT dates */ 356bbb76c12SGreg Roach $tmp = I18N::translate('interpreted %s (%s)', $d1 . $conv1, e($this->text)); 357a25f0a04SGreg Roach break; 358a25f0a04SGreg Roach case 'BEF': 359bbb76c12SGreg Roach /* I18N: Gedcom BEF dates */ 360bbb76c12SGreg Roach $tmp = I18N::translate('before %s', $d1 . $conv1); 361a25f0a04SGreg Roach break; 362a25f0a04SGreg Roach case 'AFT': 363bbb76c12SGreg Roach /* I18N: Gedcom AFT dates */ 364bbb76c12SGreg Roach $tmp = I18N::translate('after %s', $d1 . $conv1); 365a25f0a04SGreg Roach break; 366a25f0a04SGreg Roach case 'FROM': 367bbb76c12SGreg Roach /* I18N: Gedcom FROM dates */ 368bbb76c12SGreg Roach $tmp = I18N::translate('from %s', $d1 . $conv1); 369a25f0a04SGreg Roach break; 370a25f0a04SGreg Roach case 'TO': 371bbb76c12SGreg Roach /* I18N: Gedcom TO dates */ 372bbb76c12SGreg Roach $tmp = I18N::translate('to %s', $d1 . $conv1); 373a25f0a04SGreg Roach break; 374a25f0a04SGreg Roach case 'BETAND': 375bbb76c12SGreg Roach /* I18N: Gedcom BET-AND dates */ 376bbb76c12SGreg Roach $tmp = I18N::translate('between %s and %s', $d1 . $conv1, $d2 . $conv2); 377a25f0a04SGreg Roach break; 378a25f0a04SGreg Roach case 'FROMTO': 379bbb76c12SGreg Roach /* I18N: Gedcom FROM-TO dates */ 380bbb76c12SGreg Roach $tmp = I18N::translate('from %s to %s', $d1 . $conv1, $d2 . $conv2); 381a25f0a04SGreg Roach break; 382a25f0a04SGreg Roach default: 383a25f0a04SGreg Roach $tmp = I18N::translate('Invalid date'); 384bbb76c12SGreg Roach break; 385a25f0a04SGreg Roach } 386a25f0a04SGreg Roach 3875fec6c0aSGreg Roach if (strip_tags($tmp) === '') { 3885fec6c0aSGreg Roach return ''; 389a25f0a04SGreg Roach } 390b2ce94c6SRico Sonntag 391b2ce94c6SRico Sonntag return '<span class="date">' . $tmp . '</span>'; 392a25f0a04SGreg Roach } 393a25f0a04SGreg Roach 394a25f0a04SGreg Roach /** 395a25f0a04SGreg Roach * Get the earliest calendar date from this GEDCOM date. 396a25f0a04SGreg Roach * 397a25f0a04SGreg Roach * In the date “FROM 1900 TO 1910”, this would be 1900. 398a25f0a04SGreg Roach * 3994a83f5d7SGreg Roach * @return AbstractCalendarDate 400a25f0a04SGreg Roach */ 4014a83f5d7SGreg Roach public function minimumDate(): AbstractCalendarDate 402c1010edaSGreg Roach { 403a25f0a04SGreg Roach return $this->date1; 404a25f0a04SGreg Roach } 405a25f0a04SGreg Roach 406a25f0a04SGreg Roach /** 407a25f0a04SGreg Roach * Get the latest calendar date from this GEDCOM date. 408a25f0a04SGreg Roach * 409a25f0a04SGreg Roach * In the date “FROM 1900 TO 1910”, this would be 1910. 410a25f0a04SGreg Roach * 4114a83f5d7SGreg Roach * @return AbstractCalendarDate 412a25f0a04SGreg Roach */ 413e364afe4SGreg Roach public function maximumDate(): AbstractCalendarDate 414c1010edaSGreg Roach { 415af2489e5SGreg Roach return $this->date2 ?? $this->date1; 416a25f0a04SGreg Roach } 417a25f0a04SGreg Roach 418a25f0a04SGreg Roach /** 419a25f0a04SGreg Roach * Get the earliest Julian day number from this GEDCOM date. 420a25f0a04SGreg Roach * 421cbc1590aSGreg Roach * @return int 422a25f0a04SGreg Roach */ 4238f53f488SRico Sonntag public function minimumJulianDay(): int 424c1010edaSGreg Roach { 425af2489e5SGreg Roach return $this->minimumDate()->minimumJulianDay(); 426a25f0a04SGreg Roach } 427a25f0a04SGreg Roach 428a25f0a04SGreg Roach /** 429a25f0a04SGreg Roach * Get the latest Julian day number from this GEDCOM date. 430a25f0a04SGreg Roach * 431cbc1590aSGreg Roach * @return int 432a25f0a04SGreg Roach */ 4338f53f488SRico Sonntag public function maximumJulianDay(): int 434c1010edaSGreg Roach { 435af2489e5SGreg Roach return $this->maximumDate()->maximumJulianDay(); 436a25f0a04SGreg Roach } 437a25f0a04SGreg Roach 438a25f0a04SGreg Roach /** 439a25f0a04SGreg Roach * Get the middle Julian day number from the GEDCOM date. 440a25f0a04SGreg Roach * 441f5b60decSGreg Roach * For a month-only date, this would be somewhere around the 16th day. 442a25f0a04SGreg Roach * For a year-only date, this would be somewhere around 1st July. 443a25f0a04SGreg Roach * 444cbc1590aSGreg Roach * @return int 445a25f0a04SGreg Roach */ 4468f53f488SRico Sonntag public function julianDay(): int 447c1010edaSGreg Roach { 448cdaafeeeSGreg Roach return intdiv($this->minimumJulianDay() + $this->maximumJulianDay(), 2); 449a25f0a04SGreg Roach } 450a25f0a04SGreg Roach 451a25f0a04SGreg Roach /** 452a25f0a04SGreg Roach * Offset this date by N years, and round to the whole year. 453a25f0a04SGreg Roach * 454a25f0a04SGreg Roach * This is typically used to create an estimated death date, 455a25f0a04SGreg Roach * which is before a certain number of years after the birth date. 456a25f0a04SGreg Roach * 457cbc1590aSGreg Roach * @param int $years a number of years, positive or negative 458cbc1590aSGreg Roach * @param string $qualifier typically “BEF” or “AFT” 459a25f0a04SGreg Roach * 460a25f0a04SGreg Roach * @return Date 461a25f0a04SGreg Roach */ 4628f53f488SRico Sonntag public function addYears(int $years, string $qualifier = ''): Date 463c1010edaSGreg Roach { 464a25f0a04SGreg Roach $tmp = clone $this; 4654a83f5d7SGreg Roach $tmp->date1->year += $years; 4664a83f5d7SGreg Roach $tmp->date1->month = 0; 4674a83f5d7SGreg Roach $tmp->date1->day = 0; 468a25f0a04SGreg Roach $tmp->date1->setJdFromYmd(); 469a25f0a04SGreg Roach $tmp->qual1 = $qualifier; 470a25f0a04SGreg Roach $tmp->qual2 = ''; 471a25f0a04SGreg Roach $tmp->date2 = null; 472a25f0a04SGreg Roach 473a25f0a04SGreg Roach return $tmp; 474a25f0a04SGreg Roach } 475a25f0a04SGreg Roach 476a25f0a04SGreg Roach /** 4778fe32d0dSGreg Roach * Calculate the the age of a person (n years), on a given date. 478a25f0a04SGreg Roach * 479a25f0a04SGreg Roach * @param Date $d1 480a25f0a04SGreg Roach * @param Date $d2 481cbc1590aSGreg Roach * 4828fe32d0dSGreg Roach * @return int 483054771e9SGreg Roach * 484054771e9SGreg Roach * @deprecated since 2.0.4. Will be removed in 2.1.0 485a25f0a04SGreg Roach */ 4868fe32d0dSGreg Roach public static function getAgeYears(Date $d1, Date $d2): int 487c1010edaSGreg Roach { 488054771e9SGreg Roach trigger_error('Date::getAgeYears() is deprecated. Use class Age instead.', E_USER_DEPRECATED); 489054771e9SGreg Roach 4908af3e5c1SGreg Roach if (self::compare($d1, $d2) === 0) { 4918fe32d0dSGreg Roach // Overlapping dates 4928fe32d0dSGreg Roach $jd = $d1->minimumJulianDay(); 4938fe32d0dSGreg Roach } else { 4948fe32d0dSGreg Roach // Non-overlapping dates 4958fe32d0dSGreg Roach $jd = $d2->minimumJulianDay(); 4968fe32d0dSGreg Roach } 4978fe32d0dSGreg Roach 4988fe32d0dSGreg Roach if ($jd && $d1->minimumJulianDay() && $d1->minimumJulianDay() <= $jd) { 4998fe32d0dSGreg Roach return $d1->minimumDate()->getAge($jd); 5008fe32d0dSGreg Roach } 501e364afe4SGreg Roach 502e364afe4SGreg Roach return -1; 5038fe32d0dSGreg Roach } 5048fe32d0dSGreg Roach 5058fe32d0dSGreg Roach /** 5068fe32d0dSGreg Roach * Calculate the the age of a person (n days), on a given date. 5078fe32d0dSGreg Roach * 5088fe32d0dSGreg Roach * @param Date $d1 5098fe32d0dSGreg Roach * @param Date $d2 5108fe32d0dSGreg Roach * 5118fe32d0dSGreg Roach * @return int 512054771e9SGreg Roach * 513054771e9SGreg Roach * @deprecated since 2.0.4. Will be removed in 2.1.0 5148fe32d0dSGreg Roach */ 5158fe32d0dSGreg Roach public static function getAgeDays(Date $d1, Date $d2): int 5168fe32d0dSGreg Roach { 517054771e9SGreg Roach trigger_error('Date::getAgeDays() is deprecated. Use class Age instead.', E_USER_DEPRECATED); 518054771e9SGreg Roach 5191631e7f7SGreg Roach if ($d2->maximumJulianDay() >= $d1->minimumJulianDay() && $d2->minimumJulianDay() <= $d1->maximumJulianDay()) { 5208fe32d0dSGreg Roach // Overlapping dates 5218fe32d0dSGreg Roach $jd = $d1->minimumJulianDay(); 5228fe32d0dSGreg Roach } else { 5238fe32d0dSGreg Roach // Non-overlapping dates 5248fe32d0dSGreg Roach $jd = $d2->minimumJulianDay(); 5258fe32d0dSGreg Roach } 5268fe32d0dSGreg Roach 5278fe32d0dSGreg Roach // Days - integer only (for sorting, rather than for display) 5288fe32d0dSGreg Roach if ($jd && $d1->minimumJulianDay()) { 5298fe32d0dSGreg Roach return $jd - $d1->minimumJulianDay(); 5308fe32d0dSGreg Roach } 531e364afe4SGreg Roach 532e364afe4SGreg Roach return -1; 5338fe32d0dSGreg Roach } 5348fe32d0dSGreg Roach 5358fe32d0dSGreg Roach /** 5368fe32d0dSGreg Roach * Calculate the the age of a person, on a date. 5378fe32d0dSGreg Roach * 5388fe32d0dSGreg Roach * @param Date $d1 5398fe32d0dSGreg Roach * @param Date|null $d2 5408fe32d0dSGreg Roach * 5418fe32d0dSGreg Roach * @return string 542054771e9SGreg Roach * 543054771e9SGreg Roach * @deprecated since 2.0.4. Will be removed in 2.1.0 5448fe32d0dSGreg Roach */ 5458fe32d0dSGreg Roach public static function getAge(Date $d1, Date $d2 = null): string 5468fe32d0dSGreg Roach { 547054771e9SGreg Roach trigger_error('Date::getAge() is deprecated. Use class Age instead.', E_USER_DEPRECATED); 548054771e9SGreg Roach 549e364afe4SGreg Roach if ($d2 instanceof self) { 5501631e7f7SGreg Roach if ($d2->maximumJulianDay() >= $d1->minimumJulianDay() && $d2->minimumJulianDay() <= $d1->maximumJulianDay()) { 551a25f0a04SGreg Roach // Overlapping dates 552f5b60decSGreg Roach $jd = $d1->minimumJulianDay(); 553a25f0a04SGreg Roach } else { 554a25f0a04SGreg Roach // Non-overlapping dates 555f5b60decSGreg Roach $jd = $d2->minimumJulianDay(); 556a25f0a04SGreg Roach } 557a25f0a04SGreg Roach } else { 558a25f0a04SGreg Roach // If second date not specified, use today’s date 5594459dc9aSGreg Roach $jd = Carbon::now()->julianDay(); 560a25f0a04SGreg Roach } 561a25f0a04SGreg Roach 5625fec6c0aSGreg Roach // Just years, in local digits, with warning for negative/ 563f5b60decSGreg Roach if ($jd && $d1->minimumJulianDay()) { 564f5b60decSGreg Roach if ($d1->minimumJulianDay() > $jd) { 565e39fd5c6SGreg Roach return view('icons/warning'); 566e364afe4SGreg Roach } 567e364afe4SGreg Roach 5688fe32d0dSGreg Roach $years = $d1->minimumDate()->getAge($jd); 56955664801SGreg Roach 57055664801SGreg Roach return I18N::number($years); 571a25f0a04SGreg Roach } 572e364afe4SGreg Roach 57366b90994SGreg Roach return ''; 574a25f0a04SGreg Roach } 575a25f0a04SGreg Roach 576a25f0a04SGreg Roach /** 577a25f0a04SGreg Roach * Calculate the years/months/days between two events 578a25f0a04SGreg Roach * Return a gedcom style age string: "1y 2m 3d" (for fact details) 579a25f0a04SGreg Roach * 580a25f0a04SGreg Roach * @param Date $d1 581a25f0a04SGreg Roach * @param Date|null $d2 582a25f0a04SGreg Roach * 583a25f0a04SGreg Roach * @return string 584054771e9SGreg Roach * 585054771e9SGreg Roach * @deprecated since 2.0.4. Will be removed in 2.1.0 586a25f0a04SGreg Roach */ 587e364afe4SGreg Roach public static function getAgeGedcom(Date $d1, Date $d2 = null): string 588c1010edaSGreg Roach { 589054771e9SGreg Roach trigger_error('Date::getAgeGedcom() is deprecated. Use class Age instead.', E_USER_DEPRECATED); 590054771e9SGreg Roach 5918f038c36SRico Sonntag if ($d2 === null) { 5924459dc9aSGreg Roach return $d1->date1->getAgeFull(Carbon::now()->julianDay()); 5934e8e93daSGreg Roach } 5944e8e93daSGreg Roach 5954e8e93daSGreg Roach if (self::compare($d1, $d2) !== 0) { 5968fe32d0dSGreg Roach return $d1->date1->getAgeFull($d2->minimumJulianDay()); 5974e8e93daSGreg Roach } 5984e8e93daSGreg Roach 5994e8e93daSGreg Roach if ($d1->minimumJulianDay() == $d2->minimumJulianDay()) { 600a25f0a04SGreg Roach return '0d'; 6014e8e93daSGreg Roach } 6024e8e93daSGreg Roach 603a25f0a04SGreg Roach return ''; 604a25f0a04SGreg Roach } 605a25f0a04SGreg Roach 606a25f0a04SGreg Roach /** 607a25f0a04SGreg Roach * Compare two dates, so they can be sorted. 608a25f0a04SGreg Roach * 609054771e9SGreg Roach * return -1 if $a<$b 610054771e9SGreg Roach * return +1 if $b>$a 611a25f0a04SGreg Roach * return 0 if dates same/overlap 612a25f0a04SGreg Roach * BEF/AFT sort as the day before/after 613a25f0a04SGreg Roach * 614a25f0a04SGreg Roach * @param Date $a 615a25f0a04SGreg Roach * @param Date $b 616a25f0a04SGreg Roach * 617cbc1590aSGreg Roach * @return int 618a25f0a04SGreg Roach */ 619e364afe4SGreg Roach public static function compare(Date $a, Date $b): int 620c1010edaSGreg Roach { 621a25f0a04SGreg Roach // Get min/max JD for each date. 622a25f0a04SGreg Roach switch ($a->qual1) { 623a25f0a04SGreg Roach case 'BEF': 624f5b60decSGreg Roach $amin = $a->minimumJulianDay() - 1; 625a25f0a04SGreg Roach $amax = $amin; 626a25f0a04SGreg Roach break; 627a25f0a04SGreg Roach case 'AFT': 628f5b60decSGreg Roach $amax = $a->maximumJulianDay() + 1; 629a25f0a04SGreg Roach $amin = $amax; 630a25f0a04SGreg Roach break; 631a25f0a04SGreg Roach default: 632f5b60decSGreg Roach $amin = $a->minimumJulianDay(); 633f5b60decSGreg Roach $amax = $a->maximumJulianDay(); 634a25f0a04SGreg Roach break; 635a25f0a04SGreg Roach } 636a25f0a04SGreg Roach switch ($b->qual1) { 637a25f0a04SGreg Roach case 'BEF': 638f5b60decSGreg Roach $bmin = $b->minimumJulianDay() - 1; 639a25f0a04SGreg Roach $bmax = $bmin; 640a25f0a04SGreg Roach break; 641a25f0a04SGreg Roach case 'AFT': 642f5b60decSGreg Roach $bmax = $b->maximumJulianDay() + 1; 643a25f0a04SGreg Roach $bmin = $bmax; 644a25f0a04SGreg Roach break; 645a25f0a04SGreg Roach default: 646f5b60decSGreg Roach $bmin = $b->minimumJulianDay(); 647f5b60decSGreg Roach $bmax = $b->maximumJulianDay(); 648a25f0a04SGreg Roach break; 649a25f0a04SGreg Roach } 650a25f0a04SGreg Roach if ($amax < $bmin) { 651a25f0a04SGreg Roach return -1; 652a25f0a04SGreg Roach } 653b2ce94c6SRico Sonntag 654b2ce94c6SRico Sonntag if ($amin > $bmax && $bmax > 0) { 655b2ce94c6SRico Sonntag return 1; 656b2ce94c6SRico Sonntag } 657b2ce94c6SRico Sonntag 658b2ce94c6SRico Sonntag if ($amin < $bmin && $amax <= $bmax) { 659b2ce94c6SRico Sonntag return -1; 660b2ce94c6SRico Sonntag } 661b2ce94c6SRico Sonntag 662b2ce94c6SRico Sonntag if ($amin > $bmin && $amax >= $bmax && $bmax > 0) { 663b2ce94c6SRico Sonntag return 1; 664b2ce94c6SRico Sonntag } 665b2ce94c6SRico Sonntag 666b2ce94c6SRico Sonntag return 0; 667a25f0a04SGreg Roach } 668a25f0a04SGreg Roach 669a25f0a04SGreg Roach /** 670a25f0a04SGreg Roach * Check whether a gedcom date contains usable calendar date(s). 671a25f0a04SGreg Roach * 672a25f0a04SGreg Roach * An incomplete date such as "12 AUG" would be invalid, as 673a25f0a04SGreg Roach * we cannot sort it. 674a25f0a04SGreg Roach * 675cbc1590aSGreg Roach * @return bool 676a25f0a04SGreg Roach */ 6778f53f488SRico Sonntag public function isOK(): bool 678c1010edaSGreg Roach { 679f5b60decSGreg Roach return $this->minimumJulianDay() && $this->maximumJulianDay(); 680a25f0a04SGreg Roach } 681a25f0a04SGreg Roach 682a25f0a04SGreg Roach /** 683a25f0a04SGreg Roach * Calculate the gregorian year for a date. This should NOT be used internally 684a25f0a04SGreg Roach * within WT - we should keep the code "calendar neutral" to allow support for 685a25f0a04SGreg Roach * jewish/arabic users. This is only for interfacing with external entities, 686a25f0a04SGreg Roach * such as the ancestry.com search interface or the dated fact icons. 687a25f0a04SGreg Roach * 688cbc1590aSGreg Roach * @return int 689a25f0a04SGreg Roach */ 690e364afe4SGreg Roach public function gregorianYear(): int 691c1010edaSGreg Roach { 692a25f0a04SGreg Roach if ($this->isOK()) { 69359f2f229SGreg Roach $gregorian_calendar = new GregorianCalendar(); 69465e02381SGreg Roach [$year] = $gregorian_calendar->jdToYmd($this->julianDay()); 695a25f0a04SGreg Roach 696a25f0a04SGreg Roach return $year; 697a25f0a04SGreg Roach } 698b2ce94c6SRico Sonntag 699b2ce94c6SRico Sonntag return 0; 700a25f0a04SGreg Roach } 701a25f0a04SGreg Roach} 702