1a25f0a04SGreg Roach<?php 2a25f0a04SGreg Roach/** 3a25f0a04SGreg Roach * webtrees: online genealogy 41062a142SGreg Roach * Copyright (C) 2018 webtrees development team 5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 8a25f0a04SGreg Roach * (at your option) any later version. 9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12a25f0a04SGreg Roach * GNU General Public License for more details. 13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 15a25f0a04SGreg Roach */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees; 17a25f0a04SGreg Roach 18a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar; 190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\CalendarDate; 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\FrenchDate; 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\HijriDate; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JalaliDate; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate; 250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\JulianDate; 260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Date\RomanDate; 27a25f0a04SGreg Roach 28a25f0a04SGreg Roach/** 2976692c8bSGreg Roach * A representation of GEDCOM dates and date ranges. 3076692c8bSGreg Roach * 3176692c8bSGreg Roach * Since different calendars start their days at different times, (civil 32a25f0a04SGreg Roach * midnight, solar midnight, sunset, sunrise, etc.), we convert on the basis of 33a25f0a04SGreg Roach * midday. 34a25f0a04SGreg Roach * 3576692c8bSGreg Roach * We assume that years start on the first day of the first month. Where 36a25f0a04SGreg Roach * this is not the case (e.g. England prior to 1752), we need to use modified 37a25f0a04SGreg Roach * years or the OS/NS notation "4 FEB 1750/51". 38a25f0a04SGreg Roach */ 39c1010edaSGreg Roachclass Date 40c1010edaSGreg Roach{ 41a25f0a04SGreg Roach /** @var string Optional qualifier, such as BEF, FROM, ABT */ 42*fe11e66dSGreg Roach public $qual1 = ''; 43a25f0a04SGreg Roach 44a25f0a04SGreg Roach /** @var CalendarDate The first (or only) date */ 45f5b60decSGreg Roach private $date1; 46a25f0a04SGreg Roach 47a25f0a04SGreg Roach /** @var string Optional qualifier, such as TO, AND */ 48*fe11e66dSGreg Roach public $qual2 = ''; 49a25f0a04SGreg Roach 50a25f0a04SGreg Roach /** @var CalendarDate Optional second date */ 51f5b60decSGreg Roach private $date2; 52a25f0a04SGreg Roach 53a25f0a04SGreg Roach /** @var string ptional text, as included with an INTerpreted date */ 54f5b60decSGreg Roach private $text; 55a25f0a04SGreg Roach 56a25f0a04SGreg Roach /** 57a25f0a04SGreg Roach * Create a date, from GEDCOM data. 58a25f0a04SGreg Roach * 59a25f0a04SGreg Roach * @param string $date A date in GEDCOM format 60a25f0a04SGreg Roach */ 61c1010edaSGreg Roach public function __construct($date) 62c1010edaSGreg Roach { 63a25f0a04SGreg Roach // Extract any explanatory text 64a25f0a04SGreg Roach if (preg_match('/^(.*) ?[(](.*)[)]/', $date, $match)) { 65a25f0a04SGreg Roach $date = $match[1]; 66a25f0a04SGreg Roach $this->text = $match[2]; 67a25f0a04SGreg Roach } 68a25f0a04SGreg Roach if (preg_match('/^(FROM|BET) (.+) (AND|TO) (.+)/', $date, $match)) { 69a25f0a04SGreg Roach $this->qual1 = $match[1]; 70a25f0a04SGreg Roach $this->date1 = $this->parseDate($match[2]); 71a25f0a04SGreg Roach $this->qual2 = $match[3]; 72a25f0a04SGreg Roach $this->date2 = $this->parseDate($match[4]); 733506b2e5SGreg Roach } elseif (preg_match('/^(TO|FROM|BEF|AFT|CAL|EST|INT|ABT) (.+)/', $date, $match)) { 74a25f0a04SGreg Roach $this->qual1 = $match[1]; 75a25f0a04SGreg Roach $this->date1 = $this->parseDate($match[2]); 76a25f0a04SGreg Roach } else { 77a25f0a04SGreg Roach $this->date1 = $this->parseDate($date); 78a25f0a04SGreg Roach } 79a25f0a04SGreg Roach } 80a25f0a04SGreg Roach 81a25f0a04SGreg Roach /** 82a25f0a04SGreg Roach * When we copy a date object, we need to create copies of 83a25f0a04SGreg Roach * its child objects. 84a25f0a04SGreg Roach */ 85c1010edaSGreg Roach public function __clone() 86c1010edaSGreg Roach { 87a25f0a04SGreg Roach $this->date1 = clone $this->date1; 88a25f0a04SGreg Roach if (is_object($this->date2)) { 89a25f0a04SGreg Roach $this->date2 = clone $this->date2; 90a25f0a04SGreg Roach } 91a25f0a04SGreg Roach } 92a25f0a04SGreg Roach 93a25f0a04SGreg Roach /** 94a25f0a04SGreg Roach * Convert a calendar date, such as "12 JUN 1943" into calendar date object. 95a25f0a04SGreg Roach * 96a25f0a04SGreg Roach * A GEDCOM date range may have two calendar dates. 97a25f0a04SGreg Roach * 98a25f0a04SGreg Roach * @param string $date 99a25f0a04SGreg Roach * 100a25f0a04SGreg Roach * @throws \DomainException 101cbc1590aSGreg Roach * 102cbc1590aSGreg Roach * @return CalendarDate 103a25f0a04SGreg Roach */ 104c1010edaSGreg Roach private function parseDate($date) 105c1010edaSGreg Roach { 106a25f0a04SGreg Roach // Valid calendar escape specified? - use it 107bee1f90bSGreg Roach if (preg_match('/^(@#D(?:GREGORIAN|JULIAN|HEBREW|HIJRI|JALALI|FRENCH R|ROMAN)+@) ?(.*)/', $date, $match)) { 108a25f0a04SGreg Roach $cal = $match[1]; 109a25f0a04SGreg Roach $date = $match[2]; 110a25f0a04SGreg Roach } else { 111a25f0a04SGreg Roach $cal = ''; 112a25f0a04SGreg Roach } 113a25f0a04SGreg Roach // A date with a month: DM, M, MY or DMY 114a25f0a04SGreg 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)) { 115a25f0a04SGreg Roach $d = $match[1]; 116a25f0a04SGreg Roach $m = $match[2]; 117a25f0a04SGreg Roach $y = $match[3]; 118471edd97SGreg Roach } elseif (preg_match('/^(\d{1,4}(?: B\.C\.)?|\d\d\d\d\/\d\d)$/', $date, $match)) { 119471edd97SGreg Roach // A date with just a year 120a25f0a04SGreg Roach $d = ''; 121a25f0a04SGreg Roach $m = ''; 122a25f0a04SGreg Roach $y = $match[1]; 123a25f0a04SGreg Roach } else { 124a25f0a04SGreg Roach // An invalid date - do the best we can. 125a25f0a04SGreg Roach $d = ''; 126a25f0a04SGreg Roach $m = ''; 127a25f0a04SGreg Roach $y = ''; 128a25f0a04SGreg Roach // Look for a 3/4 digit year anywhere in the date 129a25f0a04SGreg Roach if (preg_match('/\b(\d{3,4})\b/', $date, $match)) { 130a25f0a04SGreg Roach $y = $match[1]; 131a25f0a04SGreg Roach } 132a25f0a04SGreg Roach // Look for a month anywhere in the date 133a25f0a04SGreg 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)) { 134a25f0a04SGreg Roach $m = $match[1]; 135a25f0a04SGreg Roach // Look for a day number anywhere in the date 136a25f0a04SGreg Roach if (preg_match('/\b(\d\d?)\b/', $date, $match)) { 137a25f0a04SGreg Roach $d = $match[1]; 138a25f0a04SGreg Roach } 139a25f0a04SGreg Roach } 140a25f0a04SGreg Roach } 141a25f0a04SGreg Roach 142a25f0a04SGreg Roach // Unambiguous dates - override calendar escape 143a25f0a04SGreg Roach if (preg_match('/^(TSH|CSH|KSL|TVT|SHV|ADR|ADS|NSN|IYR|SVN|TMZ|AAV|ELL)$/', $m)) { 144a25f0a04SGreg Roach $cal = '@#DHEBREW@'; 145a25f0a04SGreg Roach } else { 146a25f0a04SGreg Roach if (preg_match('/^(VEND|BRUM|FRIM|NIVO|PLUV|VENT|GERM|FLOR|PRAI|MESS|THER|FRUC|COMP)$/', $m)) { 147a25f0a04SGreg Roach $cal = '@#DFRENCH R@'; 148a25f0a04SGreg Roach } else { 149a25f0a04SGreg Roach if (preg_match('/^(MUHAR|SAFAR|RABI[AT]|JUMA[AT]|RAJAB|SHAAB|RAMAD|SHAWW|DHUAQ|DHUAH)$/', $m)) { 150a25f0a04SGreg Roach $cal = '@#DHIJRI@'; // This is a WT extension 151a25f0a04SGreg Roach } else { 152a25f0a04SGreg Roach if (preg_match('/^(FARVA|ORDIB|KHORD|TIR|MORDA|SHAHR|MEHR|ABAN|AZAR|DEY|BAHMA|ESFAN)$/', $m)) { 153a25f0a04SGreg Roach $cal = '@#DJALALI@'; // This is a WT extension 154a25f0a04SGreg Roach } elseif (preg_match('/^\d{1,4}( B\.C\.)|\d\d\d\d\/\d\d$/', $y)) { 155a25f0a04SGreg Roach $cal = '@#DJULIAN@'; 156a25f0a04SGreg Roach } 157a25f0a04SGreg Roach } 158a25f0a04SGreg Roach } 159a25f0a04SGreg Roach } 160a25f0a04SGreg Roach 161a25f0a04SGreg Roach // Ambiguous dates - don't override calendar escape 162a25f0a04SGreg Roach if ($cal == '') { 163a25f0a04SGreg Roach if (preg_match('/^(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)$/', $m)) { 164a25f0a04SGreg Roach $cal = '@#DGREGORIAN@'; 165a25f0a04SGreg Roach } else { 166a25f0a04SGreg Roach if (preg_match('/^[345]\d\d\d$/', $y)) { 167a25f0a04SGreg Roach // Year 3000-5999 168a25f0a04SGreg Roach $cal = '@#DHEBREW@'; 169a25f0a04SGreg Roach } else { 170a25f0a04SGreg Roach $cal = '@#DGREGORIAN@'; 171a25f0a04SGreg Roach } 172a25f0a04SGreg Roach } 173a25f0a04SGreg Roach } 174a25f0a04SGreg Roach // Now construct an object of the correct type 175a25f0a04SGreg Roach switch ($cal) { 176a25f0a04SGreg Roach case '@#DGREGORIAN@': 177c1010edaSGreg Roach return new GregorianDate([ 178c1010edaSGreg Roach $y, 179c1010edaSGreg Roach $m, 180c1010edaSGreg Roach $d, 181c1010edaSGreg Roach ]); 182a25f0a04SGreg Roach case '@#DJULIAN@': 183c1010edaSGreg Roach return new JulianDate([ 184c1010edaSGreg Roach $y, 185c1010edaSGreg Roach $m, 186c1010edaSGreg Roach $d, 187c1010edaSGreg Roach ]); 188a25f0a04SGreg Roach case '@#DHEBREW@': 189c1010edaSGreg Roach return new JewishDate([ 190c1010edaSGreg Roach $y, 191c1010edaSGreg Roach $m, 192c1010edaSGreg Roach $d, 193c1010edaSGreg Roach ]); 194a25f0a04SGreg Roach case '@#DHIJRI@': 195c1010edaSGreg Roach return new HijriDate([ 196c1010edaSGreg Roach $y, 197c1010edaSGreg Roach $m, 198c1010edaSGreg Roach $d, 199c1010edaSGreg Roach ]); 200a25f0a04SGreg Roach case '@#DFRENCH R@': 201c1010edaSGreg Roach return new FrenchDate([ 202c1010edaSGreg Roach $y, 203c1010edaSGreg Roach $m, 204c1010edaSGreg Roach $d, 205c1010edaSGreg Roach ]); 206a25f0a04SGreg Roach case '@#DJALALI@': 207c1010edaSGreg Roach return new JalaliDate([ 208c1010edaSGreg Roach $y, 209c1010edaSGreg Roach $m, 210c1010edaSGreg Roach $d, 211c1010edaSGreg Roach ]); 212a25f0a04SGreg Roach case '@#DROMAN@': 213c1010edaSGreg Roach return new RomanDate([ 214c1010edaSGreg Roach $y, 215c1010edaSGreg Roach $m, 216c1010edaSGreg Roach $d, 217c1010edaSGreg Roach ]); 218a25f0a04SGreg Roach default: 219a25f0a04SGreg 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 */ 228c1010edaSGreg Roach public static function calendarNames() 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 */ 255c1010edaSGreg Roach public function display($url = false, $date_format = null, $convert_calendars = true) 256c1010edaSGreg Roach { 25712dcbc32SGreg Roach // @TODO, This is set in index.php - but it is not safe to rely on globals. 25812dcbc32SGreg Roach global $tree; 2595d51864bSGreg Roach 26012dcbc32SGreg Roach $CALENDAR_FORMAT = $tree->getPreference('CALENDAR_FORMAT'); 261a25f0a04SGreg Roach 262a25f0a04SGreg Roach if ($date_format === null) { 263dfeee0a8SGreg Roach $date_format = I18N::dateFormat(); 264a25f0a04SGreg Roach } 265a25f0a04SGreg Roach 266a25f0a04SGreg Roach if ($convert_calendars) { 267a25f0a04SGreg Roach $calendar_format = explode('_and_', $CALENDAR_FORMAT); 268a25f0a04SGreg Roach } else { 26913abd6f3SGreg Roach $calendar_format = []; 270a25f0a04SGreg Roach } 271a25f0a04SGreg Roach 272a25f0a04SGreg Roach // Two dates with text before, between and after 273a25f0a04SGreg Roach $q1 = $this->qual1; 274a25f0a04SGreg Roach $d1 = $this->date1->format($date_format, $this->qual1); 275a25f0a04SGreg Roach $q2 = $this->qual2; 276a25f0a04SGreg Roach if (is_null($this->date2)) { 277a25f0a04SGreg Roach $d2 = ''; 278a25f0a04SGreg Roach } else { 279a25f0a04SGreg Roach $d2 = $this->date2->format($date_format, $this->qual2); 280a25f0a04SGreg Roach } 281a25f0a04SGreg Roach // Con vert to other calendars, if requested 282a25f0a04SGreg Roach $conv1 = ''; 283a25f0a04SGreg Roach $conv2 = ''; 284a25f0a04SGreg Roach foreach ($calendar_format as $cal_fmt) { 285a25f0a04SGreg Roach if ($cal_fmt != 'none') { 286a25f0a04SGreg Roach $d1conv = $this->date1->convertToCalendar($cal_fmt); 287a25f0a04SGreg Roach if ($d1conv->inValidRange()) { 288a25f0a04SGreg Roach $d1tmp = $d1conv->format($date_format, $this->qual1); 289a25f0a04SGreg Roach } else { 290a25f0a04SGreg Roach $d1tmp = ''; 291a25f0a04SGreg Roach } 292a25f0a04SGreg Roach if (is_null($this->date2)) { 293a25f0a04SGreg Roach $d2conv = null; 294a25f0a04SGreg Roach $d2tmp = ''; 295a25f0a04SGreg Roach } else { 296a25f0a04SGreg Roach $d2conv = $this->date2->convertToCalendar($cal_fmt); 297a25f0a04SGreg Roach if ($d2conv->inValidRange()) { 298a25f0a04SGreg Roach $d2tmp = $d2conv->format($date_format, $this->qual2); 299a25f0a04SGreg Roach } else { 300a25f0a04SGreg Roach $d2tmp = ''; 301a25f0a04SGreg Roach } 302a25f0a04SGreg Roach } 303d4f10423SGreg Roach // If the date is different from the unconverted date, add it to the date string. 3043cfaf201SGreg Roach if ($d1 != $d1tmp && $d1tmp !== '') { 305a25f0a04SGreg Roach if ($url) { 3063cfaf201SGreg Roach if ($CALENDAR_FORMAT !== 'none') { 307f5b8f123SGreg Roach $conv1 .= ' <span dir="' . I18N::direction() . '">(<a href="' . $d1conv->calendarUrl($date_format) . '" rel="nofollow">' . $d1tmp . '</a>)</span>'; 308a25f0a04SGreg Roach } else { 309f5b8f123SGreg Roach $conv1 .= ' <span dir="' . I18N::direction() . '"><br><a href="' . $d1conv->calendarUrl($date_format) . '" rel="nofollow">' . $d1tmp . '</a></span>'; 310a25f0a04SGreg Roach } 311a25f0a04SGreg Roach } else { 312c0af647eSGreg Roach $conv1 .= ' <span dir="' . I18N::direction() . '">(' . $d1tmp . ')</span>'; 313a25f0a04SGreg Roach } 314a25f0a04SGreg Roach } 315a25f0a04SGreg Roach if (!is_null($this->date2) && $d2 != $d2tmp && $d1tmp != '') { 316a25f0a04SGreg Roach if ($url) { 317f5b8f123SGreg Roach $conv2 .= ' <span dir="' . I18N::direction() . '">(<a href="' . $d2conv->calendarUrl($date_format) . '" rel="nofollow">' . $d2tmp . '</a>)</span>'; 318a25f0a04SGreg Roach } else { 319c0af647eSGreg Roach $conv2 .= ' <span dir="' . I18N::direction() . '">(' . $d2tmp . ')</span>'; 320a25f0a04SGreg Roach } 321a25f0a04SGreg Roach } 322a25f0a04SGreg Roach } 323a25f0a04SGreg Roach } 324a25f0a04SGreg Roach 325a25f0a04SGreg Roach // Add URLs, if requested 326a25f0a04SGreg Roach if ($url) { 327f5b8f123SGreg Roach $d1 = '<a href="' . $this->date1->calendarUrl($date_format) . '" rel="nofollow">' . $d1 . '</a>'; 328a25f0a04SGreg Roach if (!is_null($this->date2)) { 329f5b8f123SGreg Roach $d2 = '<a href="' . $this->date2->calendarUrl($date_format) . '" rel="nofollow">' . $d2 . '</a>'; 330a25f0a04SGreg Roach } 331a25f0a04SGreg Roach } 332a25f0a04SGreg Roach 333a25f0a04SGreg Roach // Localise the date 334a25f0a04SGreg Roach switch ($q1 . $q2) { 335a25f0a04SGreg Roach case '': 336a25f0a04SGreg Roach $tmp = $d1 . $conv1; 337a25f0a04SGreg Roach break; 338a25f0a04SGreg Roach case 'ABT': 339bbb76c12SGreg Roach /* I18N: Gedcom ABT dates */ 340bbb76c12SGreg Roach $tmp = I18N::translate('about %s', $d1 . $conv1); 341a25f0a04SGreg Roach break; 342a25f0a04SGreg Roach case 'CAL': 343bbb76c12SGreg Roach /* I18N: Gedcom CAL dates */ 344bbb76c12SGreg Roach $tmp = I18N::translate('calculated %s', $d1 . $conv1); 345a25f0a04SGreg Roach break; 346a25f0a04SGreg Roach case 'EST': 347bbb76c12SGreg Roach /* I18N: Gedcom EST dates */ 348bbb76c12SGreg Roach $tmp = I18N::translate('estimated %s', $d1 . $conv1); 349a25f0a04SGreg Roach break; 350a25f0a04SGreg Roach case 'INT': 351bbb76c12SGreg Roach /* I18N: Gedcom INT dates */ 352bbb76c12SGreg Roach $tmp = I18N::translate('interpreted %s (%s)', $d1 . $conv1, e($this->text)); 353a25f0a04SGreg Roach break; 354a25f0a04SGreg Roach case 'BEF': 355bbb76c12SGreg Roach /* I18N: Gedcom BEF dates */ 356bbb76c12SGreg Roach $tmp = I18N::translate('before %s', $d1 . $conv1); 357a25f0a04SGreg Roach break; 358a25f0a04SGreg Roach case 'AFT': 359bbb76c12SGreg Roach /* I18N: Gedcom AFT dates */ 360bbb76c12SGreg Roach $tmp = I18N::translate('after %s', $d1 . $conv1); 361a25f0a04SGreg Roach break; 362a25f0a04SGreg Roach case 'FROM': 363bbb76c12SGreg Roach /* I18N: Gedcom FROM dates */ 364bbb76c12SGreg Roach $tmp = I18N::translate('from %s', $d1 . $conv1); 365a25f0a04SGreg Roach break; 366a25f0a04SGreg Roach case 'TO': 367bbb76c12SGreg Roach /* I18N: Gedcom TO dates */ 368bbb76c12SGreg Roach $tmp = I18N::translate('to %s', $d1 . $conv1); 369a25f0a04SGreg Roach break; 370a25f0a04SGreg Roach case 'BETAND': 371bbb76c12SGreg Roach /* I18N: Gedcom BET-AND dates */ 372bbb76c12SGreg Roach $tmp = I18N::translate('between %s and %s', $d1 . $conv1, $d2 . $conv2); 373a25f0a04SGreg Roach break; 374a25f0a04SGreg Roach case 'FROMTO': 375bbb76c12SGreg Roach /* I18N: Gedcom FROM-TO dates */ 376bbb76c12SGreg Roach $tmp = I18N::translate('from %s to %s', $d1 . $conv1, $d2 . $conv2); 377a25f0a04SGreg Roach break; 378a25f0a04SGreg Roach default: 379a25f0a04SGreg Roach $tmp = I18N::translate('Invalid date'); 380bbb76c12SGreg Roach break; 381a25f0a04SGreg Roach } 382a25f0a04SGreg Roach if ($this->text && !$q1) { 38397455db3SGreg Roach $tmp = I18N::translate('%1$s (%2$s)', $tmp, e($this->text)); 384a25f0a04SGreg Roach } 385a25f0a04SGreg Roach 3865fec6c0aSGreg Roach if (strip_tags($tmp) === '') { 3875fec6c0aSGreg Roach return ''; 388a25f0a04SGreg Roach } else { 3895fec6c0aSGreg Roach return '<span class="date">' . $tmp . '</span>'; 390a25f0a04SGreg Roach } 391a25f0a04SGreg Roach } 392a25f0a04SGreg Roach 393a25f0a04SGreg Roach /** 394a25f0a04SGreg Roach * Get the earliest calendar date from this GEDCOM date. 395a25f0a04SGreg Roach * 396a25f0a04SGreg Roach * In the date “FROM 1900 TO 1910”, this would be 1900. 397a25f0a04SGreg Roach * 398a25f0a04SGreg Roach * @return CalendarDate 399a25f0a04SGreg Roach */ 400c1010edaSGreg Roach public function minimumDate() 401c1010edaSGreg Roach { 402a25f0a04SGreg Roach return $this->date1; 403a25f0a04SGreg Roach } 404a25f0a04SGreg Roach 405a25f0a04SGreg Roach /** 406a25f0a04SGreg Roach * Get the latest calendar date from this GEDCOM date. 407a25f0a04SGreg Roach * 408a25f0a04SGreg Roach * In the date “FROM 1900 TO 1910”, this would be 1910. 409a25f0a04SGreg Roach * 410a25f0a04SGreg Roach * @return CalendarDate 411a25f0a04SGreg Roach */ 412c1010edaSGreg Roach public function maximumDate() 413c1010edaSGreg Roach { 414a25f0a04SGreg Roach if (is_null($this->date2)) { 415a25f0a04SGreg Roach return $this->date1; 416a25f0a04SGreg Roach } else { 417a25f0a04SGreg Roach return $this->date2; 418a25f0a04SGreg Roach } 419a25f0a04SGreg Roach } 420a25f0a04SGreg Roach 421a25f0a04SGreg Roach /** 422a25f0a04SGreg Roach * Get the earliest Julian day number from this GEDCOM date. 423a25f0a04SGreg Roach * 424cbc1590aSGreg Roach * @return int 425a25f0a04SGreg Roach */ 426c1010edaSGreg Roach public function minimumJulianDay() 427c1010edaSGreg Roach { 428f5b60decSGreg Roach return $this->minimumDate()->minJD; 429a25f0a04SGreg Roach } 430a25f0a04SGreg Roach 431a25f0a04SGreg Roach /** 432a25f0a04SGreg Roach * Get the latest Julian day number from this GEDCOM date. 433a25f0a04SGreg Roach * 434cbc1590aSGreg Roach * @return int 435a25f0a04SGreg Roach */ 436c1010edaSGreg Roach public function maximumJulianDay() 437c1010edaSGreg Roach { 438f5b60decSGreg Roach return $this->maximumDate()->maxJD; 439a25f0a04SGreg Roach } 440a25f0a04SGreg Roach 441a25f0a04SGreg Roach /** 442a25f0a04SGreg Roach * Get the middle Julian day number from the GEDCOM date. 443a25f0a04SGreg Roach * 444f5b60decSGreg Roach * For a month-only date, this would be somewhere around the 16th day. 445a25f0a04SGreg Roach * For a year-only date, this would be somewhere around 1st July. 446a25f0a04SGreg Roach * 447cbc1590aSGreg Roach * @return int 448a25f0a04SGreg Roach */ 449c1010edaSGreg Roach public function julianDay() 450c1010edaSGreg Roach { 451f5b60decSGreg Roach return (int)(($this->minimumJulianDay() + $this->maximumJulianDay()) / 2); 452a25f0a04SGreg Roach } 453a25f0a04SGreg Roach 454a25f0a04SGreg Roach /** 455a25f0a04SGreg Roach * Offset this date by N years, and round to the whole year. 456a25f0a04SGreg Roach * 457a25f0a04SGreg Roach * This is typically used to create an estimated death date, 458a25f0a04SGreg Roach * which is before a certain number of years after the birth date. 459a25f0a04SGreg Roach * 460cbc1590aSGreg Roach * @param int $years a number of years, positive or negative 461cbc1590aSGreg Roach * @param string $qualifier typically “BEF” or “AFT” 462a25f0a04SGreg Roach * 463a25f0a04SGreg Roach * @return Date 464a25f0a04SGreg Roach */ 465c1010edaSGreg Roach public function addYears(int $years, string $qualifier = '') 466c1010edaSGreg Roach { 467a25f0a04SGreg Roach $tmp = clone $this; 468a25f0a04SGreg Roach $tmp->date1->y += $years; 469a25f0a04SGreg Roach $tmp->date1->m = 0; 470a25f0a04SGreg Roach $tmp->date1->d = 0; 471a25f0a04SGreg Roach $tmp->date1->setJdFromYmd(); 472a25f0a04SGreg Roach $tmp->qual1 = $qualifier; 473a25f0a04SGreg Roach $tmp->qual2 = ''; 474a25f0a04SGreg Roach $tmp->date2 = null; 475a25f0a04SGreg Roach 476a25f0a04SGreg Roach return $tmp; 477a25f0a04SGreg Roach } 478a25f0a04SGreg Roach 479a25f0a04SGreg Roach /** 480a25f0a04SGreg Roach * Calculate the the age of a person, on a date. 481a25f0a04SGreg Roach * 482a25f0a04SGreg Roach * @param Date $d1 483a25f0a04SGreg Roach * @param Date $d2 484cbc1590aSGreg Roach * @param int $format 485cbc1590aSGreg Roach * 486cbc1590aSGreg Roach * @throws \InvalidArgumentException 487a25f0a04SGreg Roach * 488a25f0a04SGreg Roach * @return int|string 489a25f0a04SGreg Roach */ 490c1010edaSGreg Roach public static function getAge(Date $d1, Date $d2 = null, $format = 0) 491c1010edaSGreg Roach { 492a25f0a04SGreg Roach if ($d2) { 493f5b60decSGreg Roach if ($d2->maximumJulianDay() >= $d1->minimumJulianDay() && $d2->minimumJulianDay() <= $d1->minimumJulianDay()) { 494a25f0a04SGreg Roach // Overlapping dates 495f5b60decSGreg Roach $jd = $d1->minimumJulianDay(); 496a25f0a04SGreg Roach } else { 497a25f0a04SGreg Roach // Non-overlapping dates 498f5b60decSGreg Roach $jd = $d2->minimumJulianDay(); 499a25f0a04SGreg Roach } 500a25f0a04SGreg Roach } else { 501a25f0a04SGreg Roach // If second date not specified, use today’s date 502a25f0a04SGreg Roach $jd = WT_CLIENT_JD; 503a25f0a04SGreg Roach } 504a25f0a04SGreg Roach 505a25f0a04SGreg Roach switch ($format) { 5065fec6c0aSGreg Roach case 0: 5075fec6c0aSGreg Roach // Years - integer only (for statistics, rather than for display) 508f5b60decSGreg Roach if ($jd && $d1->minimumJulianDay() && $d1->minimumJulianDay() <= $jd) { 509f5b60decSGreg Roach return $d1->minimumDate()->getAge(false, $jd, false); 510a25f0a04SGreg Roach } else { 511a25f0a04SGreg Roach return -1; 512a25f0a04SGreg Roach } 5135fec6c0aSGreg Roach case 1: 5145fec6c0aSGreg Roach // Days - integer only (for sorting, rather than for display) 515f5b60decSGreg Roach if ($jd && $d1->minimumJulianDay()) { 516f5b60decSGreg Roach return $jd - $d1->minimumJulianDay(); 517a25f0a04SGreg Roach } else { 518a25f0a04SGreg Roach return -1; 519a25f0a04SGreg Roach } 5205fec6c0aSGreg Roach case 2: 5215fec6c0aSGreg Roach // Just years, in local digits, with warning for negative/ 522f5b60decSGreg Roach if ($jd && $d1->minimumJulianDay()) { 523f5b60decSGreg Roach if ($d1->minimumJulianDay() > $jd) { 524a25f0a04SGreg Roach return '<i class="icon-warning"></i>'; 525a25f0a04SGreg Roach } else { 526f7c952a6SGreg Roach return I18N::number($d1->minimumDate()->getAge(false, $jd, true)); 527a25f0a04SGreg Roach } 528a25f0a04SGreg Roach } else { 52966b90994SGreg Roach return ''; 530a25f0a04SGreg Roach } 531a25f0a04SGreg Roach default: 532a25f0a04SGreg Roach throw new \InvalidArgumentException('format: ' . $format); 533a25f0a04SGreg Roach } 534a25f0a04SGreg Roach } 535a25f0a04SGreg Roach 536a25f0a04SGreg Roach /** 537a25f0a04SGreg Roach * Calculate the years/months/days between two events 538a25f0a04SGreg Roach * Return a gedcom style age string: "1y 2m 3d" (for fact details) 539a25f0a04SGreg Roach * 540a25f0a04SGreg Roach * @param Date $d1 541a25f0a04SGreg Roach * @param Date|null $d2 542a25f0a04SGreg Roach * 543a25f0a04SGreg Roach * @return string 544a25f0a04SGreg Roach */ 545c1010edaSGreg Roach public static function getAgeGedcom(Date $d1, Date $d2 = null) 546c1010edaSGreg Roach { 547a25f0a04SGreg Roach if (is_null($d2)) { 5480120b44aSGreg Roach return $d1->date1->getAge(true, WT_CLIENT_JD, true); 549a25f0a04SGreg Roach } else { 550a25f0a04SGreg Roach // If dates overlap, then can’t calculate age. 551f5b60decSGreg Roach if (self::compare($d1, $d2)) { 5520120b44aSGreg Roach return $d1->date1->getAge(true, $d2->minimumJulianDay(), true); 553b851b295SGreg Roach } elseif (self::compare($d1, $d2) == 0 && $d1->minimumJulianDay() == $d2->minimumJulianDay()) { 554a25f0a04SGreg Roach return '0d'; 555a25f0a04SGreg Roach } else { 556a25f0a04SGreg Roach return ''; 557a25f0a04SGreg Roach } 558a25f0a04SGreg Roach } 559a25f0a04SGreg Roach } 560a25f0a04SGreg Roach 561a25f0a04SGreg Roach /** 562a25f0a04SGreg Roach * Compare two dates, so they can be sorted. 563a25f0a04SGreg Roach * 564a25f0a04SGreg Roach * return <0 if $a<$b 565a25f0a04SGreg Roach * return >0 if $b>$a 566a25f0a04SGreg Roach * return 0 if dates same/overlap 567a25f0a04SGreg Roach * BEF/AFT sort as the day before/after 568a25f0a04SGreg Roach * 569a25f0a04SGreg Roach * @param Date $a 570a25f0a04SGreg Roach * @param Date $b 571a25f0a04SGreg Roach * 572cbc1590aSGreg Roach * @return int 573a25f0a04SGreg Roach */ 574c1010edaSGreg Roach public static function compare(Date $a, Date $b) 575c1010edaSGreg Roach { 576a25f0a04SGreg Roach // Get min/max JD for each date. 577a25f0a04SGreg Roach switch ($a->qual1) { 578a25f0a04SGreg Roach case 'BEF': 579f5b60decSGreg Roach $amin = $a->minimumJulianDay() - 1; 580a25f0a04SGreg Roach $amax = $amin; 581a25f0a04SGreg Roach break; 582a25f0a04SGreg Roach case 'AFT': 583f5b60decSGreg Roach $amax = $a->maximumJulianDay() + 1; 584a25f0a04SGreg Roach $amin = $amax; 585a25f0a04SGreg Roach break; 586a25f0a04SGreg Roach default: 587f5b60decSGreg Roach $amin = $a->minimumJulianDay(); 588f5b60decSGreg Roach $amax = $a->maximumJulianDay(); 589a25f0a04SGreg Roach break; 590a25f0a04SGreg Roach } 591a25f0a04SGreg Roach switch ($b->qual1) { 592a25f0a04SGreg Roach case 'BEF': 593f5b60decSGreg Roach $bmin = $b->minimumJulianDay() - 1; 594a25f0a04SGreg Roach $bmax = $bmin; 595a25f0a04SGreg Roach break; 596a25f0a04SGreg Roach case 'AFT': 597f5b60decSGreg Roach $bmax = $b->maximumJulianDay() + 1; 598a25f0a04SGreg Roach $bmin = $bmax; 599a25f0a04SGreg Roach break; 600a25f0a04SGreg Roach default: 601f5b60decSGreg Roach $bmin = $b->minimumJulianDay(); 602f5b60decSGreg Roach $bmax = $b->maximumJulianDay(); 603a25f0a04SGreg Roach break; 604a25f0a04SGreg Roach } 605a25f0a04SGreg Roach if ($amax < $bmin) { 606a25f0a04SGreg Roach return -1; 607a25f0a04SGreg Roach } elseif ($amin > $bmax && $bmax > 0) { 608a25f0a04SGreg Roach return 1; 609a25f0a04SGreg Roach } elseif ($amin < $bmin && $amax <= $bmax) { 610a25f0a04SGreg Roach return -1; 611a25f0a04SGreg Roach } elseif ($amin > $bmin && $amax >= $bmax && $bmax > 0) { 612a25f0a04SGreg Roach return 1; 613a25f0a04SGreg Roach } else { 614a25f0a04SGreg Roach return 0; 615a25f0a04SGreg Roach } 616a25f0a04SGreg Roach } 617a25f0a04SGreg Roach 618a25f0a04SGreg Roach /** 619a25f0a04SGreg Roach * Check whether a gedcom date contains usable calendar date(s). 620a25f0a04SGreg Roach * 621a25f0a04SGreg Roach * An incomplete date such as "12 AUG" would be invalid, as 622a25f0a04SGreg Roach * we cannot sort it. 623a25f0a04SGreg Roach * 624cbc1590aSGreg Roach * @return bool 625a25f0a04SGreg Roach */ 626c1010edaSGreg Roach public function isOK() 627c1010edaSGreg Roach { 628f5b60decSGreg Roach return $this->minimumJulianDay() && $this->maximumJulianDay(); 629a25f0a04SGreg Roach } 630a25f0a04SGreg Roach 631a25f0a04SGreg Roach /** 632a25f0a04SGreg Roach * Calculate the gregorian year for a date. This should NOT be used internally 633a25f0a04SGreg Roach * within WT - we should keep the code "calendar neutral" to allow support for 634a25f0a04SGreg Roach * jewish/arabic users. This is only for interfacing with external entities, 635a25f0a04SGreg Roach * such as the ancestry.com search interface or the dated fact icons. 636a25f0a04SGreg Roach * 637cbc1590aSGreg Roach * @return int 638a25f0a04SGreg Roach */ 639c1010edaSGreg Roach public function gregorianYear() 640c1010edaSGreg Roach { 641a25f0a04SGreg Roach if ($this->isOK()) { 64259f2f229SGreg Roach $gregorian_calendar = new GregorianCalendar(); 643f5b60decSGreg Roach list($year) = $gregorian_calendar->jdToYmd($this->julianDay()); 644a25f0a04SGreg Roach 645a25f0a04SGreg Roach return $year; 646a25f0a04SGreg Roach } else { 647a25f0a04SGreg Roach return 0; 648a25f0a04SGreg Roach } 649a25f0a04SGreg Roach } 650a25f0a04SGreg Roach} 651