xref: /webtrees/app/Date.php (revision 76692c8b291f16d9251d67f27078779f6737fe7e)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
4a25f0a04SGreg Roach * Copyright (C) 2015 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 */
16*76692c8bSGreg 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/**
29*76692c8bSGreg Roach * A representation of GEDCOM dates and date ranges.
30*76692c8bSGreg Roach *
31*76692c8bSGreg 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 *
35*76692c8bSGreg 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 */
39a25f0a04SGreg Roachclass Date {
40a25f0a04SGreg Roach	/** @var string Optional qualifier, such as BEF, FROM, ABT */
41c6310c76SGreg Roach	public $qual1;
42a25f0a04SGreg Roach
43a25f0a04SGreg Roach	/** @var CalendarDate  The first (or only) date */
44f5b60decSGreg Roach	private $date1;
45a25f0a04SGreg Roach
46a25f0a04SGreg Roach	/** @var string  Optional qualifier, such as TO, AND*/
47c6310c76SGreg Roach	public $qual2;
48a25f0a04SGreg Roach
49a25f0a04SGreg Roach	/** @var CalendarDate Optional second date */
50f5b60decSGreg Roach	private $date2;
51a25f0a04SGreg Roach
52a25f0a04SGreg Roach	/** @var string ptional text, as included with an INTerpreted date */
53f5b60decSGreg Roach	private $text;
54a25f0a04SGreg Roach
55a25f0a04SGreg Roach	/**
56a25f0a04SGreg Roach	 * Create a date, from GEDCOM data.
57a25f0a04SGreg Roach	 *
58a25f0a04SGreg Roach	 * @param string $date A date in GEDCOM format
59a25f0a04SGreg Roach	 */
60e2d2f0ebSGreg Roach	public function __construct($date) {
61a25f0a04SGreg Roach		// Extract any explanatory text
62a25f0a04SGreg Roach		if (preg_match('/^(.*) ?[(](.*)[)]/', $date, $match)) {
63a25f0a04SGreg Roach			$date       = $match[1];
64a25f0a04SGreg Roach			$this->text = $match[2];
65a25f0a04SGreg Roach		}
66a25f0a04SGreg Roach		if (preg_match('/^(FROM|BET) (.+) (AND|TO) (.+)/', $date, $match)) {
67a25f0a04SGreg Roach			$this->qual1 = $match[1];
68a25f0a04SGreg Roach			$this->date1 = $this->parseDate($match[2]);
69a25f0a04SGreg Roach			$this->qual2 = $match[3];
70a25f0a04SGreg Roach			$this->date2 = $this->parseDate($match[4]);
713506b2e5SGreg Roach		} elseif (preg_match('/^(TO|FROM|BEF|AFT|CAL|EST|INT|ABT) (.+)/', $date, $match)) {
72a25f0a04SGreg Roach			$this->qual1 = $match[1];
73a25f0a04SGreg Roach			$this->date1 = $this->parseDate($match[2]);
74a25f0a04SGreg Roach		} else {
75a25f0a04SGreg Roach			$this->date1 = $this->parseDate($date);
76a25f0a04SGreg Roach		}
77a25f0a04SGreg Roach	}
78a25f0a04SGreg Roach
79a25f0a04SGreg Roach	/**
80a25f0a04SGreg Roach	 * When we copy a date object, we need to create copies of
81a25f0a04SGreg Roach	 * its child objects.
82a25f0a04SGreg Roach	 */
83f5b60decSGreg Roach	public function __clone() {
84a25f0a04SGreg Roach		$this->date1 = clone $this->date1;
85a25f0a04SGreg Roach		if (is_object($this->date2)) {
86a25f0a04SGreg Roach			$this->date2 = clone $this->date2;
87a25f0a04SGreg Roach		}
88a25f0a04SGreg Roach	}
89a25f0a04SGreg Roach
90a25f0a04SGreg Roach	/**
91a25f0a04SGreg Roach	 * Convert a calendar date, such as "12 JUN 1943" into calendar date object.
92a25f0a04SGreg Roach	 *
93a25f0a04SGreg Roach	 * A GEDCOM date range may have two calendar dates.
94a25f0a04SGreg Roach	 *
95a25f0a04SGreg Roach	 * @param string $date
96a25f0a04SGreg Roach	 *
97a25f0a04SGreg Roach	 * @throws \DomainException
98cbc1590aSGreg Roach	 *
99cbc1590aSGreg Roach	 * @return CalendarDate
100a25f0a04SGreg Roach	 */
101f5b60decSGreg Roach	private function parseDate($date) {
102a25f0a04SGreg Roach		// Valid calendar escape specified? - use it
103bee1f90bSGreg Roach		if (preg_match('/^(@#D(?:GREGORIAN|JULIAN|HEBREW|HIJRI|JALALI|FRENCH R|ROMAN)+@) ?(.*)/', $date, $match)) {
104a25f0a04SGreg Roach			$cal  = $match[1];
105a25f0a04SGreg Roach			$date = $match[2];
106a25f0a04SGreg Roach		} else {
107a25f0a04SGreg Roach			$cal = '';
108a25f0a04SGreg Roach		}
109a25f0a04SGreg Roach		// A date with a month: DM, M, MY or DMY
110a25f0a04SGreg 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)) {
111a25f0a04SGreg Roach			$d = $match[1];
112a25f0a04SGreg Roach			$m = $match[2];
113a25f0a04SGreg Roach			$y = $match[3];
114a25f0a04SGreg Roach		} else // A date with just a year
115a25f0a04SGreg Roach			if (preg_match('/^(\d{1,4}(?: B\.C\.)?|\d\d\d\d\/\d\d)$/', $date, $match)) {
116a25f0a04SGreg Roach				$d = '';
117a25f0a04SGreg Roach				$m = '';
118a25f0a04SGreg Roach				$y = $match[1];
119a25f0a04SGreg Roach			} else {
120a25f0a04SGreg Roach				// An invalid date - do the best we can.
121a25f0a04SGreg Roach				$d = '';
122a25f0a04SGreg Roach				$m = '';
123a25f0a04SGreg Roach				$y = '';
124a25f0a04SGreg Roach				// Look for a 3/4 digit year anywhere in the date
125a25f0a04SGreg Roach				if (preg_match('/\b(\d{3,4})\b/', $date, $match)) {
126a25f0a04SGreg Roach					$y = $match[1];
127a25f0a04SGreg Roach				}
128a25f0a04SGreg Roach				// Look for a month anywhere in the date
129a25f0a04SGreg 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)) {
130a25f0a04SGreg Roach					$m = $match[1];
131a25f0a04SGreg Roach					// Look for a day number anywhere in the date
132a25f0a04SGreg Roach					if (preg_match('/\b(\d\d?)\b/', $date, $match)) {
133a25f0a04SGreg Roach						$d = $match[1];
134a25f0a04SGreg Roach					}
135a25f0a04SGreg Roach				}
136a25f0a04SGreg Roach			}
137a25f0a04SGreg Roach
138a25f0a04SGreg Roach		// Unambiguous dates - override calendar escape
139a25f0a04SGreg Roach		if (preg_match('/^(TSH|CSH|KSL|TVT|SHV|ADR|ADS|NSN|IYR|SVN|TMZ|AAV|ELL)$/', $m)) {
140a25f0a04SGreg Roach			$cal = '@#DHEBREW@';
141a25f0a04SGreg Roach		} else {
142a25f0a04SGreg Roach			if (preg_match('/^(VEND|BRUM|FRIM|NIVO|PLUV|VENT|GERM|FLOR|PRAI|MESS|THER|FRUC|COMP)$/', $m)) {
143a25f0a04SGreg Roach				$cal = '@#DFRENCH R@';
144a25f0a04SGreg Roach			} else {
145a25f0a04SGreg Roach				if (preg_match('/^(MUHAR|SAFAR|RABI[AT]|JUMA[AT]|RAJAB|SHAAB|RAMAD|SHAWW|DHUAQ|DHUAH)$/', $m)) {
146a25f0a04SGreg Roach					$cal = '@#DHIJRI@'; // This is a WT extension
147a25f0a04SGreg Roach				} else {
148a25f0a04SGreg Roach					if (preg_match('/^(FARVA|ORDIB|KHORD|TIR|MORDA|SHAHR|MEHR|ABAN|AZAR|DEY|BAHMA|ESFAN)$/', $m)) {
149a25f0a04SGreg Roach						$cal = '@#DJALALI@'; // This is a WT extension
150a25f0a04SGreg Roach					} elseif (preg_match('/^\d{1,4}( B\.C\.)|\d\d\d\d\/\d\d$/', $y)) {
151a25f0a04SGreg Roach						$cal = '@#DJULIAN@';
152a25f0a04SGreg Roach					}
153a25f0a04SGreg Roach				}
154a25f0a04SGreg Roach			}
155a25f0a04SGreg Roach		}
156a25f0a04SGreg Roach
157a25f0a04SGreg Roach		// Ambiguous dates - don't override calendar escape
158a25f0a04SGreg Roach		if ($cal == '') {
159a25f0a04SGreg Roach			if (preg_match('/^(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)$/', $m)) {
160a25f0a04SGreg Roach				$cal = '@#DGREGORIAN@';
161a25f0a04SGreg Roach			} else {
162a25f0a04SGreg Roach				if (preg_match('/^[345]\d\d\d$/', $y)) {
163a25f0a04SGreg Roach					// Year 3000-5999
164a25f0a04SGreg Roach					$cal = '@#DHEBREW@';
165a25f0a04SGreg Roach				} else {
166a25f0a04SGreg Roach					$cal = '@#DGREGORIAN@';
167a25f0a04SGreg Roach				}
168a25f0a04SGreg Roach			}
169a25f0a04SGreg Roach		}
170a25f0a04SGreg Roach		// Now construct an object of the correct type
171a25f0a04SGreg Roach		switch ($cal) {
172a25f0a04SGreg Roach		case '@#DGREGORIAN@':
173a25f0a04SGreg Roach			return new GregorianDate(array($y, $m, $d));
174a25f0a04SGreg Roach		case '@#DJULIAN@':
175a25f0a04SGreg Roach			return new JulianDate(array($y, $m, $d));
176a25f0a04SGreg Roach		case '@#DHEBREW@':
177a25f0a04SGreg Roach			return new JewishDate(array($y, $m, $d));
178a25f0a04SGreg Roach		case '@#DHIJRI@':
179a25f0a04SGreg Roach			return new HijriDate(array($y, $m, $d));
180a25f0a04SGreg Roach		case '@#DFRENCH R@':
181a25f0a04SGreg Roach			return new FrenchDate(array($y, $m, $d));
182a25f0a04SGreg Roach		case '@#DJALALI@':
183a25f0a04SGreg Roach			return new JalaliDate(array($y, $m, $d));
184a25f0a04SGreg Roach		case '@#DROMAN@':
185a25f0a04SGreg Roach			return new RomanDate(array($y, $m, $d));
186a25f0a04SGreg Roach		default:
187a25f0a04SGreg Roach			throw new \DomainException('Invalid calendar');
188a25f0a04SGreg Roach		}
189a25f0a04SGreg Roach	}
190a25f0a04SGreg Roach
1914e6225d5SGreg Roach	/**
1924e6225d5SGreg Roach	 * A list of supported calendars and their names.
1934e6225d5SGreg Roach	 *
1944e6225d5SGreg Roach	 * @return string[]
1954e6225d5SGreg Roach	 */
1964e6225d5SGreg Roach	public static function calendarNames() {
1974e6225d5SGreg Roach		return array(
1984e6225d5SGreg Roach			'gregorian' => /* I18N: The gregorian calendar */ I18N::translate('Gregorian'),
1994e6225d5SGreg Roach			'julian'    => /* I18N: The julian calendar */ I18N::translate('Julian'),
2004e6225d5SGreg Roach			'french'    => /* I18N: The French calendar */ I18N::translate('French'),
2014e6225d5SGreg Roach			'jewish'    => /* I18N: The Hebrew/Jewish calendar */ I18N::translate('Jewish'),
2024e6225d5SGreg Roach			'hijri'     => /* I18N: The Arabic/Hijri calendar */ I18N::translate('Hijri'),
2034e6225d5SGreg Roach			'jalali'    => /* I18N: The Persian/Jalali calendar */ I18N::translate('Jalali'),
2044e6225d5SGreg Roach		);
2054e6225d5SGreg Roach	}
2064e6225d5SGreg Roach
207a25f0a04SGreg Roach	/**
208a25f0a04SGreg Roach	 * Convert a date to the preferred format and calendar(s) display.
209a25f0a04SGreg Roach	 *
210cbc1590aSGreg Roach	 * @param bool|null   $url               Wrap the date in a link to calendar.php
211a25f0a04SGreg Roach	 * @param string|null $date_format       Override the default date format
212cbc1590aSGreg Roach	 * @param bool|null   $convert_calendars Convert the date into other calendars
213a25f0a04SGreg Roach	 *
214a25f0a04SGreg Roach	 * @return string
215a25f0a04SGreg Roach	 */
216f5b60decSGreg Roach	public function display($url = false, $date_format = null, $convert_calendars = true) {
2173cfaf201SGreg Roach		global $WT_TREE;
2185d51864bSGreg Roach
2199f712a0dSGreg Roach		// Search engines do not get links to the calendar pages
2209f712a0dSGreg Roach		if (Auth::isSearchEngine()) {
2219f712a0dSGreg Roach			$url = false;
2229f712a0dSGreg Roach		}
2239f712a0dSGreg Roach
2245d51864bSGreg Roach		$CALENDAR_FORMAT = $WT_TREE->getPreference('CALENDAR_FORMAT');
225a25f0a04SGreg Roach
226a25f0a04SGreg Roach		if ($date_format === null) {
227dfeee0a8SGreg Roach			$date_format = I18N::dateFormat();
228a25f0a04SGreg Roach		}
229a25f0a04SGreg Roach
230a25f0a04SGreg Roach		if ($convert_calendars) {
231a25f0a04SGreg Roach			$calendar_format = explode('_and_', $CALENDAR_FORMAT);
232a25f0a04SGreg Roach		} else {
233a25f0a04SGreg Roach			$calendar_format = array();
234a25f0a04SGreg Roach		}
235a25f0a04SGreg Roach
236a25f0a04SGreg Roach		// Two dates with text before, between and after
237a25f0a04SGreg Roach		$q1 = $this->qual1;
238a25f0a04SGreg Roach		$d1 = $this->date1->format($date_format, $this->qual1);
239a25f0a04SGreg Roach		$q2 = $this->qual2;
240a25f0a04SGreg Roach		if (is_null($this->date2)) {
241a25f0a04SGreg Roach			$d2 = '';
242a25f0a04SGreg Roach		} else {
243a25f0a04SGreg Roach			$d2 = $this->date2->format($date_format, $this->qual2);
244a25f0a04SGreg Roach		}
245a25f0a04SGreg Roach		// Con vert to other calendars, if requested
246a25f0a04SGreg Roach		$conv1 = '';
247a25f0a04SGreg Roach		$conv2 = '';
248a25f0a04SGreg Roach		foreach ($calendar_format as $cal_fmt) {
249a25f0a04SGreg Roach			if ($cal_fmt != 'none') {
250a25f0a04SGreg Roach				$d1conv = $this->date1->convertToCalendar($cal_fmt);
251a25f0a04SGreg Roach				if ($d1conv->inValidRange()) {
252a25f0a04SGreg Roach					$d1tmp = $d1conv->format($date_format, $this->qual1);
253a25f0a04SGreg Roach				} else {
254a25f0a04SGreg Roach					$d1tmp = '';
255a25f0a04SGreg Roach				}
256a25f0a04SGreg Roach				if (is_null($this->date2)) {
257a25f0a04SGreg Roach					$d2conv = null;
258a25f0a04SGreg Roach					$d2tmp  = '';
259a25f0a04SGreg Roach				} else {
260a25f0a04SGreg Roach					$d2conv = $this->date2->convertToCalendar($cal_fmt);
261a25f0a04SGreg Roach					if ($d2conv->inValidRange()) {
262a25f0a04SGreg Roach						$d2tmp = $d2conv->format($date_format, $this->qual2);
263a25f0a04SGreg Roach					} else {
264a25f0a04SGreg Roach						$d2tmp = '';
265a25f0a04SGreg Roach					}
266a25f0a04SGreg Roach				}
267d4f10423SGreg Roach				// If the date is different from the unconverted date, add it to the date string.
2683cfaf201SGreg Roach				if ($d1 != $d1tmp && $d1tmp !== '') {
269a25f0a04SGreg Roach					if ($url) {
2703cfaf201SGreg Roach						if ($CALENDAR_FORMAT !== 'none') {
271c0af647eSGreg Roach							$conv1 .= ' <span dir="' . I18N::direction() . '">(<a href="' . $d1conv->calendarUrl($date_format) . '">' . $d1tmp . '</a>)</span>';
272a25f0a04SGreg Roach						} else {
273c0af647eSGreg Roach							$conv1 .= ' <span dir="' . I18N::direction() . '"><br><a href="' . $d1conv->calendarUrl($date_format) . '">' . $d1tmp . '</a></span>';
274a25f0a04SGreg Roach						}
275a25f0a04SGreg Roach					} else {
276c0af647eSGreg Roach						$conv1 .= ' <span dir="' . I18N::direction() . '">(' . $d1tmp . ')</span>';
277a25f0a04SGreg Roach					}
278a25f0a04SGreg Roach				}
279a25f0a04SGreg Roach				if (!is_null($this->date2) && $d2 != $d2tmp && $d1tmp != '') {
280a25f0a04SGreg Roach					if ($url) {
281c0af647eSGreg Roach						$conv2 .= ' <span dir="' . I18N::direction() . '">(<a href="' . $d2conv->calendarUrl($date_format) . '">' . $d2tmp . '</a>)</span>';
282a25f0a04SGreg Roach					} else {
283c0af647eSGreg Roach						$conv2 .= ' <span dir="' . I18N::direction() . '">(' . $d2tmp . ')</span>';
284a25f0a04SGreg Roach					}
285a25f0a04SGreg Roach				}
286a25f0a04SGreg Roach			}
287a25f0a04SGreg Roach		}
288a25f0a04SGreg Roach
289a25f0a04SGreg Roach		// Add URLs, if requested
290a25f0a04SGreg Roach		if ($url) {
291a25f0a04SGreg Roach			$d1 = '<a href="' . $this->date1->calendarUrl($date_format) . '">' . $d1 . '</a>';
292a25f0a04SGreg Roach			if (!is_null($this->date2)) {
293a25f0a04SGreg Roach				$d2 = '<a href="' . $this->date2->calendarUrl($date_format) . '">' . $d2 . '</a>';
294a25f0a04SGreg Roach			}
295a25f0a04SGreg Roach		}
296a25f0a04SGreg Roach
297a25f0a04SGreg Roach		// Localise the date
298a25f0a04SGreg Roach		switch ($q1 . $q2) {
299a25f0a04SGreg Roach		case '':
300a25f0a04SGreg Roach			$tmp = $d1 . $conv1;
301a25f0a04SGreg Roach			break;
302a25f0a04SGreg Roach		case 'ABT':
303a25f0a04SGreg Roach			$tmp = /* I18N: Gedcom ABT dates */ I18N::translate('about %s', $d1 . $conv1);
304a25f0a04SGreg Roach			break;
305a25f0a04SGreg Roach		case 'CAL':
306a25f0a04SGreg Roach			$tmp = /* I18N: Gedcom CAL dates */ I18N::translate('calculated %s', $d1 . $conv1);
307a25f0a04SGreg Roach			break;
308a25f0a04SGreg Roach		case 'EST':
309a25f0a04SGreg Roach			$tmp = /* I18N: Gedcom EST dates */ I18N::translate('estimated %s', $d1 . $conv1);
310a25f0a04SGreg Roach			break;
311a25f0a04SGreg Roach		case 'INT':
312a25f0a04SGreg Roach			$tmp = /* I18N: Gedcom INT dates */ I18N::translate('interpreted %s (%s)', $d1 . $conv1, $this->text);
313a25f0a04SGreg Roach			break;
314a25f0a04SGreg Roach		case 'BEF':
315a25f0a04SGreg Roach			$tmp = /* I18N: Gedcom BEF dates */ I18N::translate('before %s', $d1 . $conv1);
316a25f0a04SGreg Roach			break;
317a25f0a04SGreg Roach		case 'AFT':
318a25f0a04SGreg Roach			$tmp = /* I18N: Gedcom AFT dates */ I18N::translate('after %s', $d1 . $conv1);
319a25f0a04SGreg Roach			break;
320a25f0a04SGreg Roach		case 'FROM':
321a25f0a04SGreg Roach			$tmp = /* I18N: Gedcom FROM dates */ I18N::translate('from %s', $d1 . $conv1);
322a25f0a04SGreg Roach			break;
323a25f0a04SGreg Roach		case 'TO':
324a25f0a04SGreg Roach			$tmp = /* I18N: Gedcom TO dates */ I18N::translate('to %s', $d1 . $conv1);
325a25f0a04SGreg Roach			break;
326a25f0a04SGreg Roach		case 'BETAND':
327a25f0a04SGreg Roach			$tmp = /* I18N: Gedcom BET-AND dates */ I18N::translate('between %s and %s', $d1 . $conv1, $d2 . $conv2);
328a25f0a04SGreg Roach			break;
329a25f0a04SGreg Roach		case 'FROMTO':
330a25f0a04SGreg Roach			$tmp = /* I18N: Gedcom FROM-TO dates */ I18N::translate('from %s to %s', $d1 . $conv1, $d2 . $conv2);
331a25f0a04SGreg Roach			break;
332a25f0a04SGreg Roach		default:
333a25f0a04SGreg Roach			$tmp = I18N::translate('Invalid date');
334a25f0a04SGreg Roach			break; // e.g. BET without AND
335a25f0a04SGreg Roach		}
336a25f0a04SGreg Roach		if ($this->text && !$q1) {
337a25f0a04SGreg Roach			$tmp = I18N::translate('%1$s (%2$s)', $tmp, $this->text);
338a25f0a04SGreg Roach		}
339a25f0a04SGreg Roach
3405fec6c0aSGreg Roach		if (strip_tags($tmp) === '') {
3415fec6c0aSGreg Roach			return '';
342a25f0a04SGreg Roach		} else {
3435fec6c0aSGreg Roach			return '<span class="date">' . $tmp . '</span>';
344a25f0a04SGreg Roach		}
345a25f0a04SGreg Roach	}
346a25f0a04SGreg Roach
347a25f0a04SGreg Roach	/**
348a25f0a04SGreg Roach	 * Get the earliest calendar date from this GEDCOM date.
349a25f0a04SGreg Roach	 *
350a25f0a04SGreg Roach	 * In the date “FROM 1900 TO 1910”, this would be 1900.
351a25f0a04SGreg Roach	 *
352a25f0a04SGreg Roach	 * @return CalendarDate
353a25f0a04SGreg Roach	 */
354f5b60decSGreg Roach	public function minimumDate() {
355a25f0a04SGreg Roach		return $this->date1;
356a25f0a04SGreg Roach	}
357a25f0a04SGreg Roach
358a25f0a04SGreg Roach	/**
359a25f0a04SGreg Roach	 * Get the latest calendar date from this GEDCOM date.
360a25f0a04SGreg Roach	 *
361a25f0a04SGreg Roach	 * In the date “FROM 1900 TO 1910”, this would be 1910.
362a25f0a04SGreg Roach	 *
363a25f0a04SGreg Roach	 * @return CalendarDate
364a25f0a04SGreg Roach	 */
365f5b60decSGreg Roach	public function maximumDate() {
366a25f0a04SGreg Roach		if (is_null($this->date2)) {
367a25f0a04SGreg Roach			return $this->date1;
368a25f0a04SGreg Roach		} else {
369a25f0a04SGreg Roach			return $this->date2;
370a25f0a04SGreg Roach		}
371a25f0a04SGreg Roach	}
372a25f0a04SGreg Roach
373a25f0a04SGreg Roach	/**
374a25f0a04SGreg Roach	 * Get the earliest Julian day number from this GEDCOM date.
375a25f0a04SGreg Roach	 *
376cbc1590aSGreg Roach	 * @return int
377a25f0a04SGreg Roach	 */
378f5b60decSGreg Roach	public function minimumJulianDay() {
379f5b60decSGreg Roach		return $this->minimumDate()->minJD;
380a25f0a04SGreg Roach	}
381a25f0a04SGreg Roach
382a25f0a04SGreg Roach	/**
383a25f0a04SGreg Roach	 * Get the latest Julian day number from this GEDCOM date.
384a25f0a04SGreg Roach	 *
385cbc1590aSGreg Roach	 * @return int
386a25f0a04SGreg Roach	 */
387f5b60decSGreg Roach	public function maximumJulianDay() {
388f5b60decSGreg Roach		return $this->maximumDate()->maxJD;
389a25f0a04SGreg Roach	}
390a25f0a04SGreg Roach
391a25f0a04SGreg Roach	/**
392a25f0a04SGreg Roach	 * Get the middle Julian day number from the GEDCOM date.
393a25f0a04SGreg Roach	 *
394f5b60decSGreg Roach	 * For a month-only date, this would be somewhere around the 16th day.
395a25f0a04SGreg Roach	 * For a year-only date, this would be somewhere around 1st July.
396a25f0a04SGreg Roach	 *
397cbc1590aSGreg Roach	 * @return int
398a25f0a04SGreg Roach	 */
399f5b60decSGreg Roach	public function julianDay() {
400f5b60decSGreg Roach		return (int) (($this->minimumJulianDay() + $this->maximumJulianDay()) / 2);
401a25f0a04SGreg Roach	}
402a25f0a04SGreg Roach
403a25f0a04SGreg Roach	/**
404a25f0a04SGreg Roach	 * Offset this date by N years, and round to the whole year.
405a25f0a04SGreg Roach	 *
406a25f0a04SGreg Roach	 * This is typically used to create an estimated death date,
407a25f0a04SGreg Roach	 * which is before a certain number of years after the birth date.
408a25f0a04SGreg Roach	 *
409cbc1590aSGreg Roach	 * @param int     $years     a number of years, positive or negative
410cbc1590aSGreg Roach	 * @param string  $qualifier typically “BEF” or “AFT”
411a25f0a04SGreg Roach	 *
412a25f0a04SGreg Roach	 * @return Date
413a25f0a04SGreg Roach	 */
414f5b60decSGreg Roach	public function addYears($years, $qualifier = '') {
415a25f0a04SGreg Roach		$tmp = clone $this;
416a25f0a04SGreg Roach		$tmp->date1->y += $years;
417a25f0a04SGreg Roach		$tmp->date1->m = 0;
418a25f0a04SGreg Roach		$tmp->date1->d = 0;
419a25f0a04SGreg Roach		$tmp->date1->setJdFromYmd();
420a25f0a04SGreg Roach		$tmp->qual1 = $qualifier;
421a25f0a04SGreg Roach		$tmp->qual2 = '';
422a25f0a04SGreg Roach		$tmp->date2 = null;
423a25f0a04SGreg Roach
424a25f0a04SGreg Roach		return $tmp;
425a25f0a04SGreg Roach	}
426a25f0a04SGreg Roach
427a25f0a04SGreg Roach	/**
428a25f0a04SGreg Roach	 * Calculate the the age of a person, on a date.
429a25f0a04SGreg Roach	 *
430a25f0a04SGreg Roach	 * @param Date $d1
431a25f0a04SGreg Roach	 * @param Date $d2
432cbc1590aSGreg Roach	 * @param int  $format
433cbc1590aSGreg Roach	 *
434cbc1590aSGreg Roach	 * @throws \InvalidArgumentException
435a25f0a04SGreg Roach	 *
436a25f0a04SGreg Roach	 * @return int|string
437a25f0a04SGreg Roach	 */
438f5b60decSGreg Roach	public static function getAge(Date $d1, Date $d2 = null, $format = 0) {
439a25f0a04SGreg Roach		if ($d2) {
440f5b60decSGreg Roach			if ($d2->maximumJulianDay() >= $d1->minimumJulianDay() && $d2->minimumJulianDay() <= $d1->minimumJulianDay()) {
441a25f0a04SGreg Roach				// Overlapping dates
442f5b60decSGreg Roach				$jd = $d1->minimumJulianDay();
443a25f0a04SGreg Roach			} else {
444a25f0a04SGreg Roach				// Non-overlapping dates
445f5b60decSGreg Roach				$jd = $d2->minimumJulianDay();
446a25f0a04SGreg Roach			}
447a25f0a04SGreg Roach		} else {
448a25f0a04SGreg Roach			// If second date not specified, use today’s date
449a25f0a04SGreg Roach			$jd = WT_CLIENT_JD;
450a25f0a04SGreg Roach		}
451a25f0a04SGreg Roach
452a25f0a04SGreg Roach		switch ($format) {
4535fec6c0aSGreg Roach		case 0:
4545fec6c0aSGreg Roach			// Years - integer only (for statistics, rather than for display)
455f5b60decSGreg Roach			if ($jd && $d1->minimumJulianDay() && $d1->minimumJulianDay() <= $jd) {
456f5b60decSGreg Roach				return $d1->minimumDate()->getAge(false, $jd, false);
457a25f0a04SGreg Roach			} else {
458a25f0a04SGreg Roach				return -1;
459a25f0a04SGreg Roach			}
4605fec6c0aSGreg Roach		case 1:
4615fec6c0aSGreg Roach			// Days - integer only (for sorting, rather than for display)
462f5b60decSGreg Roach			if ($jd && $d1->minimumJulianDay()) {
463f5b60decSGreg Roach				return $jd - $d1->minimumJulianDay();
464a25f0a04SGreg Roach			} else {
465a25f0a04SGreg Roach				return -1;
466a25f0a04SGreg Roach			}
4675fec6c0aSGreg Roach		case 2:
4685fec6c0aSGreg Roach			// Just years, in local digits, with warning for negative/
469f5b60decSGreg Roach			if ($jd && $d1->minimumJulianDay()) {
470f5b60decSGreg Roach				if ($d1->minimumJulianDay() > $jd) {
471a25f0a04SGreg Roach					return '<i class="icon-warning"></i>';
472a25f0a04SGreg Roach				} else {
473f5b60decSGreg Roach					return I18N::number($d1->minimumDate()->getAge(false, $jd));
474a25f0a04SGreg Roach				}
475a25f0a04SGreg Roach			} else {
476a25f0a04SGreg Roach				return '&nbsp;';
477a25f0a04SGreg Roach			}
478a25f0a04SGreg Roach		default:
479a25f0a04SGreg Roach			throw new \InvalidArgumentException('format: ' . $format);
480a25f0a04SGreg Roach		}
481a25f0a04SGreg Roach	}
482a25f0a04SGreg Roach
483a25f0a04SGreg Roach	/**
484a25f0a04SGreg Roach	 * Calculate the years/months/days between two events
485a25f0a04SGreg Roach	 * Return a gedcom style age string: "1y 2m 3d" (for fact details)
486a25f0a04SGreg Roach	 *
487a25f0a04SGreg Roach	 * @param Date      $d1
488a25f0a04SGreg Roach	 * @param Date|null $d2
489cbc1590aSGreg Roach	 * @param bool      $warn_on_negative
490a25f0a04SGreg Roach	 *
491a25f0a04SGreg Roach	 * @return string
492a25f0a04SGreg Roach	 */
4937091fc7dSGreg Roach	public static function getAgeGedcom(Date $d1, Date $d2 = null, $warn_on_negative = true) {
494a25f0a04SGreg Roach		if (is_null($d2)) {
4957091fc7dSGreg Roach			return $d1->date1->getAge(true, WT_CLIENT_JD, $warn_on_negative);
496a25f0a04SGreg Roach		} else {
497a25f0a04SGreg Roach			// If dates overlap, then can’t calculate age.
498f5b60decSGreg Roach			if (self::compare($d1, $d2)) {
4997091fc7dSGreg Roach				return $d1->date1->getAge(true, $d2->minimumJulianDay(), $warn_on_negative);
500f5b60decSGreg Roach			} elseif (self::compare($d1, $d2) == 0 && $d1->date1->minJD == $d2->minimumJulianDay()) {
501a25f0a04SGreg Roach				return '0d';
502a25f0a04SGreg Roach			} else {
503a25f0a04SGreg Roach				return '';
504a25f0a04SGreg Roach			}
505a25f0a04SGreg Roach		}
506a25f0a04SGreg Roach	}
507a25f0a04SGreg Roach
508a25f0a04SGreg Roach	/**
509a25f0a04SGreg Roach	 * Compare two dates, so they can be sorted.
510a25f0a04SGreg Roach	 *
511a25f0a04SGreg Roach	 * return <0 if $a<$b
512a25f0a04SGreg Roach	 * return >0 if $b>$a
513a25f0a04SGreg Roach	 * return  0 if dates same/overlap
514a25f0a04SGreg Roach	 * BEF/AFT sort as the day before/after
515a25f0a04SGreg Roach	 *
516a25f0a04SGreg Roach	 * @param Date $a
517a25f0a04SGreg Roach	 * @param Date $b
518a25f0a04SGreg Roach	 *
519cbc1590aSGreg Roach	 * @return int
520a25f0a04SGreg Roach	 */
521f5b60decSGreg Roach	public static function compare(Date $a, Date $b) {
522a25f0a04SGreg Roach		// Get min/max JD for each date.
523a25f0a04SGreg Roach		switch ($a->qual1) {
524a25f0a04SGreg Roach		case 'BEF':
525f5b60decSGreg Roach			$amin = $a->minimumJulianDay() - 1;
526a25f0a04SGreg Roach			$amax = $amin;
527a25f0a04SGreg Roach			break;
528a25f0a04SGreg Roach		case 'AFT':
529f5b60decSGreg Roach			$amax = $a->maximumJulianDay() + 1;
530a25f0a04SGreg Roach			$amin = $amax;
531a25f0a04SGreg Roach			break;
532a25f0a04SGreg Roach		default:
533f5b60decSGreg Roach			$amin = $a->minimumJulianDay();
534f5b60decSGreg Roach			$amax = $a->maximumJulianDay();
535a25f0a04SGreg Roach			break;
536a25f0a04SGreg Roach		}
537a25f0a04SGreg Roach		switch ($b->qual1) {
538a25f0a04SGreg Roach		case 'BEF':
539f5b60decSGreg Roach			$bmin = $b->minimumJulianDay() - 1;
540a25f0a04SGreg Roach			$bmax = $bmin;
541a25f0a04SGreg Roach			break;
542a25f0a04SGreg Roach		case 'AFT':
543f5b60decSGreg Roach			$bmax = $b->maximumJulianDay() + 1;
544a25f0a04SGreg Roach			$bmin = $bmax;
545a25f0a04SGreg Roach			break;
546a25f0a04SGreg Roach		default:
547f5b60decSGreg Roach			$bmin = $b->minimumJulianDay();
548f5b60decSGreg Roach			$bmax = $b->maximumJulianDay();
549a25f0a04SGreg Roach			break;
550a25f0a04SGreg Roach		}
551a25f0a04SGreg Roach		if ($amax < $bmin) {
552a25f0a04SGreg Roach			return -1;
553a25f0a04SGreg Roach		} elseif ($amin > $bmax && $bmax > 0) {
554a25f0a04SGreg Roach			return 1;
555a25f0a04SGreg Roach		} elseif ($amin < $bmin && $amax <= $bmax) {
556a25f0a04SGreg Roach			return -1;
557a25f0a04SGreg Roach		} elseif ($amin > $bmin && $amax >= $bmax && $bmax > 0) {
558a25f0a04SGreg Roach			return 1;
559a25f0a04SGreg Roach		} else {
560a25f0a04SGreg Roach			return 0;
561a25f0a04SGreg Roach		}
562a25f0a04SGreg Roach	}
563a25f0a04SGreg Roach
564a25f0a04SGreg Roach	/**
565a25f0a04SGreg Roach	 * Check whether a gedcom date contains usable calendar date(s).
566a25f0a04SGreg Roach	 *
567a25f0a04SGreg Roach	 * An incomplete date such as "12 AUG" would be invalid, as
568a25f0a04SGreg Roach	 * we cannot sort it.
569a25f0a04SGreg Roach	 *
570cbc1590aSGreg Roach	 * @return bool
571a25f0a04SGreg Roach	 */
572f5b60decSGreg Roach	public function isOK() {
573f5b60decSGreg Roach		return $this->minimumJulianDay() && $this->maximumJulianDay();
574a25f0a04SGreg Roach	}
575a25f0a04SGreg Roach
576a25f0a04SGreg Roach	/**
577a25f0a04SGreg Roach	 * Calculate the gregorian year for a date.  This should NOT be used internally
578a25f0a04SGreg Roach	 * within WT - we should keep the code "calendar neutral" to allow support for
579a25f0a04SGreg Roach	 * jewish/arabic users.  This is only for interfacing with external entities,
580a25f0a04SGreg Roach	 * such as the ancestry.com search interface or the dated fact icons.
581a25f0a04SGreg Roach	 *
582cbc1590aSGreg Roach	 * @return int
583a25f0a04SGreg Roach	 */
584f5b60decSGreg Roach	public function gregorianYear() {
585a25f0a04SGreg Roach		if ($this->isOK()) {
586a25f0a04SGreg Roach			$gregorian_calendar = new GregorianCalendar;
587f5b60decSGreg Roach			list($year)         = $gregorian_calendar->jdToYmd($this->julianDay());
588a25f0a04SGreg Roach
589a25f0a04SGreg Roach			return $year;
590a25f0a04SGreg Roach		} else {
591a25f0a04SGreg Roach			return 0;
592a25f0a04SGreg Roach		}
593a25f0a04SGreg Roach	}
594a25f0a04SGreg Roach}
595