xref: /webtrees/app/Date.php (revision 5fec6c0a91ca5b48737343f0c80bcad73648fa41)
1a25f0a04SGreg Roach<?php
2dd04c183SGreg Roachnamespace Fisharebest\Webtrees;
3a25f0a04SGreg Roach
4a25f0a04SGreg Roach/**
5a25f0a04SGreg Roach * webtrees: online genealogy
6a25f0a04SGreg Roach * Copyright (C) 2015 webtrees development team
7a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
8a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
9a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
10a25f0a04SGreg Roach * (at your option) any later version.
11a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
12a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
13a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14a25f0a04SGreg Roach * GNU General Public License for more details.
15a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
16a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
17a25f0a04SGreg Roach */
18a25f0a04SGreg Roach
19a25f0a04SGreg Roach// Classes for Gedcom Date/Calendar functionality.
20a25f0a04SGreg Roach//
21a25f0a04SGreg Roach//
22a25f0a04SGreg Roach// webtrees: online genealogy
23a25f0a04SGreg Roach// Copyright (C) 2014 Greg Roach
24a25f0a04SGreg Roach//
25a25f0a04SGreg Roach// This program is free software; you can redistribute it and/or modify
26a25f0a04SGreg Roach// it under the terms of the GNU General Public License as published by
27a25f0a04SGreg Roach// the Free Software Foundation; either version 2 of the License, or
28a25f0a04SGreg Roach// (at your option) any later version.
29a25f0a04SGreg Roach//
30a25f0a04SGreg Roach// This program is distributed in the hope that it will be useful,
31a25f0a04SGreg Roach// but WITHOUT ANY WARRANTY; without even the implied warranty of
32a25f0a04SGreg Roach// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33a25f0a04SGreg Roach// GNU General Public License for more details.
34a25f0a04SGreg Roach//
35a25f0a04SGreg Roach// You should have received a copy of the GNU General Public License
36a25f0a04SGreg Roach// along with this program; if not, write to the Free Software
37a25f0a04SGreg Roach// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
38a25f0a04SGreg Roach
39a25f0a04SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
40a25f0a04SGreg Roach
41a25f0a04SGreg Roach/**
42a25f0a04SGreg Roach * Class Date - a representation of GEDCOM dates and date ranges
43a25f0a04SGreg Roach * NOTE: Since different calendars start their days at different times, (civil
44a25f0a04SGreg Roach * midnight, solar midnight, sunset, sunrise, etc.), we convert on the basis of
45a25f0a04SGreg Roach * midday.
46a25f0a04SGreg Roach *
47a25f0a04SGreg Roach * NOTE: We assume that years start on the first day of the first month.  Where
48a25f0a04SGreg Roach * this is not the case (e.g. England prior to 1752), we need to use modified
49a25f0a04SGreg Roach * years or the OS/NS notation "4 FEB 1750/51".
50a25f0a04SGreg Roach */
51a25f0a04SGreg Roachclass Date {
52a25f0a04SGreg Roach	/** @var string Optional qualifier, such as BEF, FROM, ABT */
53a25f0a04SGreg Roach	var $qual1;
54a25f0a04SGreg Roach
55a25f0a04SGreg Roach	/** @var CalendarDate  The first (or only) date */
56a25f0a04SGreg Roach	var $date1;
57a25f0a04SGreg Roach
58a25f0a04SGreg Roach	/** @var string  Optional qualifier, such as TO, AND*/
59a25f0a04SGreg Roach	var $qual2;
60a25f0a04SGreg Roach
61a25f0a04SGreg Roach	/** @var CalendarDate Optional second date */
62a25f0a04SGreg Roach	var $date2;
63a25f0a04SGreg Roach
64a25f0a04SGreg Roach	/** @var string ptional text, as included with an INTerpreted date */
65a25f0a04SGreg Roach	var $text;
66a25f0a04SGreg Roach
67a25f0a04SGreg Roach	/**
68a25f0a04SGreg Roach	 * Create a date, from GEDCOM data.
69a25f0a04SGreg Roach	 *
70a25f0a04SGreg Roach	 * @param string $date A date in GEDCOM format
71a25f0a04SGreg Roach	 */
72a25f0a04SGreg Roach	function __construct($date) {
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]);
83a25f0a04SGreg Roach		} elseif (preg_match('/^(FROM|BET|TO|AND|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	 */
95a25f0a04SGreg Roach	function __clone() {
96a25f0a04SGreg Roach		$this->date1 = clone $this->date1;
97a25f0a04SGreg Roach		if (is_object($this->date2)) {
98a25f0a04SGreg Roach			$this->date2 = clone $this->date2;
99a25f0a04SGreg Roach		}
100a25f0a04SGreg Roach	}
101a25f0a04SGreg Roach
102a25f0a04SGreg Roach	/**
103a25f0a04SGreg Roach	 * Convert a calendar date, such as "12 JUN 1943" into calendar date object.
104a25f0a04SGreg Roach	 *
105a25f0a04SGreg Roach	 * A GEDCOM date range may have two calendar dates.
106a25f0a04SGreg Roach	 *
107a25f0a04SGreg Roach	 * @param string $date
108a25f0a04SGreg Roach	 *
109a25f0a04SGreg Roach	 * @return CalendarDate
110a25f0a04SGreg Roach	 * @throws \DomainException
111a25f0a04SGreg Roach	 */
112a25f0a04SGreg Roach	static function parseDate($date) {
113a25f0a04SGreg Roach		// Valid calendar escape specified? - use it
114a25f0a04SGreg Roach		if (preg_match('/^(@#D(?:GREGORIAN|JULIAN|HEBREW|HIJRI|JALALI|FRENCH R|ROMAN|JALALI)+@) ?(.*)/', $date, $match)) {
115a25f0a04SGreg Roach			$cal  = $match[1];
116a25f0a04SGreg Roach			$date = $match[2];
117a25f0a04SGreg Roach		} else {
118a25f0a04SGreg Roach			$cal = '';
119a25f0a04SGreg Roach		}
120a25f0a04SGreg Roach		// A date with a month: DM, M, MY or DMY
121a25f0a04SGreg 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)) {
122a25f0a04SGreg Roach			$d = $match[1];
123a25f0a04SGreg Roach			$m = $match[2];
124a25f0a04SGreg Roach			$y = $match[3];
125a25f0a04SGreg Roach		} else // A date with just a year
126a25f0a04SGreg Roach			if (preg_match('/^(\d{1,4}(?: B\.C\.)?|\d\d\d\d\/\d\d)$/', $date, $match)) {
127a25f0a04SGreg Roach				$d = '';
128a25f0a04SGreg Roach				$m = '';
129a25f0a04SGreg Roach				$y = $match[1];
130a25f0a04SGreg Roach			} else {
131a25f0a04SGreg Roach				// An invalid date - do the best we can.
132a25f0a04SGreg Roach				$d = '';
133a25f0a04SGreg Roach				$m = '';
134a25f0a04SGreg Roach				$y = '';
135a25f0a04SGreg Roach				// Look for a 3/4 digit year anywhere in the date
136a25f0a04SGreg Roach				if (preg_match('/\b(\d{3,4})\b/', $date, $match)) {
137a25f0a04SGreg Roach					$y = $match[1];
138a25f0a04SGreg Roach				}
139a25f0a04SGreg Roach				// Look for a month anywhere in the date
140a25f0a04SGreg 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)) {
141a25f0a04SGreg Roach					$m = $match[1];
142a25f0a04SGreg Roach					// Look for a day number anywhere in the date
143a25f0a04SGreg Roach					if (preg_match('/\b(\d\d?)\b/', $date, $match)) {
144a25f0a04SGreg Roach						$d = $match[1];
145a25f0a04SGreg Roach					}
146a25f0a04SGreg Roach				}
147a25f0a04SGreg Roach			}
148a25f0a04SGreg Roach
149a25f0a04SGreg Roach		// Unambiguous dates - override calendar escape
150a25f0a04SGreg Roach		if (preg_match('/^(TSH|CSH|KSL|TVT|SHV|ADR|ADS|NSN|IYR|SVN|TMZ|AAV|ELL)$/', $m)) {
151a25f0a04SGreg Roach			$cal = '@#DHEBREW@';
152a25f0a04SGreg Roach		} else {
153a25f0a04SGreg Roach			if (preg_match('/^(VEND|BRUM|FRIM|NIVO|PLUV|VENT|GERM|FLOR|PRAI|MESS|THER|FRUC|COMP)$/', $m)) {
154a25f0a04SGreg Roach				$cal = '@#DFRENCH R@';
155a25f0a04SGreg Roach			} else {
156a25f0a04SGreg Roach				if (preg_match('/^(MUHAR|SAFAR|RABI[AT]|JUMA[AT]|RAJAB|SHAAB|RAMAD|SHAWW|DHUAQ|DHUAH)$/', $m)) {
157a25f0a04SGreg Roach					$cal = '@#DHIJRI@'; // This is a WT extension
158a25f0a04SGreg Roach				} else {
159a25f0a04SGreg Roach					if (preg_match('/^(FARVA|ORDIB|KHORD|TIR|MORDA|SHAHR|MEHR|ABAN|AZAR|DEY|BAHMA|ESFAN)$/', $m)) {
160a25f0a04SGreg Roach						$cal = '@#DJALALI@'; // This is a WT extension
161a25f0a04SGreg Roach					} elseif (preg_match('/^\d{1,4}( B\.C\.)|\d\d\d\d\/\d\d$/', $y)) {
162a25f0a04SGreg Roach						$cal = '@#DJULIAN@';
163a25f0a04SGreg Roach					}
164a25f0a04SGreg Roach				}
165a25f0a04SGreg Roach			}
166a25f0a04SGreg Roach		}
167a25f0a04SGreg Roach
168a25f0a04SGreg Roach		// Ambiguous dates - don't override calendar escape
169a25f0a04SGreg Roach		if ($cal == '') {
170a25f0a04SGreg Roach			if (preg_match('/^(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)$/', $m)) {
171a25f0a04SGreg Roach				$cal = '@#DGREGORIAN@';
172a25f0a04SGreg Roach			} else {
173a25f0a04SGreg Roach				if (preg_match('/^[345]\d\d\d$/', $y)) {
174a25f0a04SGreg Roach					// Year 3000-5999
175a25f0a04SGreg Roach					$cal = '@#DHEBREW@';
176a25f0a04SGreg Roach				} else {
177a25f0a04SGreg Roach					$cal = '@#DGREGORIAN@';
178a25f0a04SGreg Roach				}
179a25f0a04SGreg Roach			}
180a25f0a04SGreg Roach		}
181a25f0a04SGreg Roach		// Now construct an object of the correct type
182a25f0a04SGreg Roach		switch ($cal) {
183a25f0a04SGreg Roach		case '@#DGREGORIAN@':
184a25f0a04SGreg Roach			return new GregorianDate(array($y, $m, $d));
185a25f0a04SGreg Roach		case '@#DJULIAN@':
186a25f0a04SGreg Roach			return new JulianDate(array($y, $m, $d));
187a25f0a04SGreg Roach		case '@#DHEBREW@':
188a25f0a04SGreg Roach			return new JewishDate(array($y, $m, $d));
189a25f0a04SGreg Roach		case '@#DHIJRI@':
190a25f0a04SGreg Roach			return new HijriDate(array($y, $m, $d));
191a25f0a04SGreg Roach		case '@#DFRENCH R@':
192a25f0a04SGreg Roach			return new FrenchDate(array($y, $m, $d));
193a25f0a04SGreg Roach		case '@#DJALALI@':
194a25f0a04SGreg Roach			return new JalaliDate(array($y, $m, $d));
195a25f0a04SGreg Roach		case '@#DROMAN@':
196a25f0a04SGreg Roach			return new RomanDate(array($y, $m, $d));
197a25f0a04SGreg Roach		default:
198a25f0a04SGreg Roach			throw new \DomainException('Invalid calendar');
199a25f0a04SGreg Roach		}
200a25f0a04SGreg Roach	}
201a25f0a04SGreg Roach
202a25f0a04SGreg Roach	/**
203a25f0a04SGreg Roach	 * Convert a date to the preferred format and calendar(s) display.
204a25f0a04SGreg Roach	 *
205a25f0a04SGreg Roach	 * @param boolean|null $url               Wrap the date in a link to calendar.php
206a25f0a04SGreg Roach	 * @param string|null  $date_format       Override the default date format
207a25f0a04SGreg Roach	 * @param boolean|null $convert_calendars Convert the date into other calendars
208a25f0a04SGreg Roach	 *
209a25f0a04SGreg Roach	 * @return string
210a25f0a04SGreg Roach	 */
211a25f0a04SGreg Roach	function display($url = false, $date_format = null, $convert_calendars = true) {
2125d51864bSGreg Roach		global $TEXT_DIRECTION, $WT_TREE;
2135d51864bSGreg Roach
2145d51864bSGreg Roach		$CALENDAR_FORMAT = $WT_TREE->getPreference('CALENDAR_FORMAT');
215a25f0a04SGreg Roach
216a25f0a04SGreg Roach		if ($date_format === null) {
217*5fec6c0aSGreg Roach			$date_format = /* I18N: This is the format string for full dates.  See http://php.net/date for codes */ I18N::noop('%j %F %Y');
218a25f0a04SGreg Roach		}
219a25f0a04SGreg Roach
220a25f0a04SGreg Roach		if ($convert_calendars) {
221a25f0a04SGreg Roach			$calendar_format = explode('_and_', $CALENDAR_FORMAT);
222a25f0a04SGreg Roach		} else {
223a25f0a04SGreg Roach			$calendar_format = array();
224a25f0a04SGreg Roach		}
225a25f0a04SGreg Roach
226a25f0a04SGreg Roach		// Two dates with text before, between and after
227a25f0a04SGreg Roach		$q1 = $this->qual1;
228a25f0a04SGreg Roach		$d1 = $this->date1->format($date_format, $this->qual1);
229a25f0a04SGreg Roach		$q2 = $this->qual2;
230a25f0a04SGreg Roach		if (is_null($this->date2)) {
231a25f0a04SGreg Roach			$d2 = '';
232a25f0a04SGreg Roach		} else {
233a25f0a04SGreg Roach			$d2 = $this->date2->format($date_format, $this->qual2);
234a25f0a04SGreg Roach		}
235a25f0a04SGreg Roach		// Con vert to other calendars, if requested
236a25f0a04SGreg Roach		$conv1 = '';
237a25f0a04SGreg Roach		$conv2 = '';
238a25f0a04SGreg Roach		foreach ($calendar_format as $cal_fmt) {
239a25f0a04SGreg Roach			if ($cal_fmt != 'none') {
240a25f0a04SGreg Roach				$d1conv = $this->date1->convertToCalendar($cal_fmt);
241a25f0a04SGreg Roach				if ($d1conv->inValidRange()) {
242a25f0a04SGreg Roach					$d1tmp = $d1conv->format($date_format, $this->qual1);
243a25f0a04SGreg Roach				} else {
244a25f0a04SGreg Roach					$d1tmp = '';
245a25f0a04SGreg Roach				}
246a25f0a04SGreg Roach				if (is_null($this->date2)) {
247a25f0a04SGreg Roach					$d2conv = null;
248a25f0a04SGreg Roach					$d2tmp  = '';
249a25f0a04SGreg Roach				} else {
250a25f0a04SGreg Roach					$d2conv = $this->date2->convertToCalendar($cal_fmt);
251a25f0a04SGreg Roach					if ($d2conv->inValidRange()) {
252a25f0a04SGreg Roach						$d2tmp = $d2conv->format($date_format, $this->qual2);
253a25f0a04SGreg Roach					} else {
254a25f0a04SGreg Roach						$d2tmp = '';
255a25f0a04SGreg Roach					}
256a25f0a04SGreg Roach				}
257a25f0a04SGreg Roach				// If the date is different to the unconverted date, add it to the date string.
258a25f0a04SGreg Roach				if ($d1 != $d1tmp && $d1tmp != '') {
259a25f0a04SGreg Roach					if ($url) {
260a25f0a04SGreg Roach						if ($CALENDAR_FORMAT != "none") {
261a25f0a04SGreg Roach							$conv1 .= ' <span dir="' . $TEXT_DIRECTION . '">(<a href="' . $d1conv->calendarUrl($date_format) . '">' . $d1tmp . '</a>)</span>';
262a25f0a04SGreg Roach						} else {
263a25f0a04SGreg Roach							$conv1 .= ' <span dir="' . $TEXT_DIRECTION . '"><br><a href="' . $d1conv->calendarUrl($date_format) . '">' . $d1tmp . '</a></span>';
264a25f0a04SGreg Roach						}
265a25f0a04SGreg Roach					} else {
266a25f0a04SGreg Roach						$conv1 .= ' <span dir="' . $TEXT_DIRECTION . '">(' . $d1tmp . ')</span>';
267a25f0a04SGreg Roach					}
268a25f0a04SGreg Roach				}
269a25f0a04SGreg Roach				if (!is_null($this->date2) && $d2 != $d2tmp && $d1tmp != '') {
270a25f0a04SGreg Roach					if ($url) {
271a25f0a04SGreg Roach						$conv2 .= ' <span dir="' . $TEXT_DIRECTION . '">(<a href="' . $d2conv->calendarUrl($date_format) . '">' . $d2tmp . '</a>)</span>';
272a25f0a04SGreg Roach					} else {
273a25f0a04SGreg Roach						$conv2 .= ' <span dir="' . $TEXT_DIRECTION . '">(' . $d2tmp . ')</span>';
274a25f0a04SGreg Roach					}
275a25f0a04SGreg Roach				}
276a25f0a04SGreg Roach			}
277a25f0a04SGreg Roach		}
278a25f0a04SGreg Roach
279a25f0a04SGreg Roach		// Add URLs, if requested
280a25f0a04SGreg Roach		if ($url) {
281a25f0a04SGreg Roach			$d1 = '<a href="' . $this->date1->calendarUrl($date_format) . '">' . $d1 . '</a>';
282a25f0a04SGreg Roach			if (!is_null($this->date2)) {
283a25f0a04SGreg Roach				$d2 = '<a href="' . $this->date2->calendarUrl($date_format) . '">' . $d2 . '</a>';
284a25f0a04SGreg Roach			}
285a25f0a04SGreg Roach		}
286a25f0a04SGreg Roach
287a25f0a04SGreg Roach		// Localise the date
288a25f0a04SGreg Roach		switch ($q1 . $q2) {
289a25f0a04SGreg Roach		case '':
290a25f0a04SGreg Roach			$tmp = $d1 . $conv1;
291a25f0a04SGreg Roach			break;
292a25f0a04SGreg Roach		case 'ABT':
293a25f0a04SGreg Roach			$tmp = /* I18N: Gedcom ABT dates */ I18N::translate('about %s', $d1 . $conv1);
294a25f0a04SGreg Roach			break;
295a25f0a04SGreg Roach		case 'CAL':
296a25f0a04SGreg Roach			$tmp = /* I18N: Gedcom CAL dates */ I18N::translate('calculated %s', $d1 . $conv1);
297a25f0a04SGreg Roach			break;
298a25f0a04SGreg Roach		case 'EST':
299a25f0a04SGreg Roach			$tmp = /* I18N: Gedcom EST dates */ I18N::translate('estimated %s', $d1 . $conv1);
300a25f0a04SGreg Roach			break;
301a25f0a04SGreg Roach		case 'INT':
302a25f0a04SGreg Roach			$tmp = /* I18N: Gedcom INT dates */ I18N::translate('interpreted %s (%s)', $d1 . $conv1, $this->text);
303a25f0a04SGreg Roach			break;
304a25f0a04SGreg Roach		case 'BEF':
305a25f0a04SGreg Roach			$tmp = /* I18N: Gedcom BEF dates */ I18N::translate('before %s', $d1 . $conv1);
306a25f0a04SGreg Roach			break;
307a25f0a04SGreg Roach		case 'AFT':
308a25f0a04SGreg Roach			$tmp = /* I18N: Gedcom AFT dates */ I18N::translate('after %s', $d1 . $conv1);
309a25f0a04SGreg Roach			break;
310a25f0a04SGreg Roach		case 'FROM':
311a25f0a04SGreg Roach			$tmp = /* I18N: Gedcom FROM dates */ I18N::translate('from %s', $d1 . $conv1);
312a25f0a04SGreg Roach			break;
313a25f0a04SGreg Roach		case 'TO':
314a25f0a04SGreg Roach			$tmp = /* I18N: Gedcom TO dates */ I18N::translate('to %s', $d1 . $conv1);
315a25f0a04SGreg Roach			break;
316a25f0a04SGreg Roach		case 'BETAND':
317a25f0a04SGreg Roach			$tmp = /* I18N: Gedcom BET-AND dates */ I18N::translate('between %s and %s', $d1 . $conv1, $d2 . $conv2);
318a25f0a04SGreg Roach			break;
319a25f0a04SGreg Roach		case 'FROMTO':
320a25f0a04SGreg Roach			$tmp = /* I18N: Gedcom FROM-TO dates */ I18N::translate('from %s to %s', $d1 . $conv1, $d2 . $conv2);
321a25f0a04SGreg Roach			break;
322a25f0a04SGreg Roach		default:
323a25f0a04SGreg Roach			$tmp = I18N::translate('Invalid date');
324a25f0a04SGreg Roach			break; // e.g. BET without AND
325a25f0a04SGreg Roach		}
326a25f0a04SGreg Roach		if ($this->text && !$q1) {
327a25f0a04SGreg Roach			$tmp = I18N::translate('%1$s (%2$s)', $tmp, $this->text);
328a25f0a04SGreg Roach		}
329a25f0a04SGreg Roach
330*5fec6c0aSGreg Roach		if (strip_tags($tmp) === '') {
331*5fec6c0aSGreg Roach			return '';
332a25f0a04SGreg Roach		} else {
333*5fec6c0aSGreg Roach			return '<span class="date">' . $tmp . '</span>';
334a25f0a04SGreg Roach		}
335a25f0a04SGreg Roach	}
336a25f0a04SGreg Roach
337a25f0a04SGreg Roach	/**
338a25f0a04SGreg Roach	 * Get the earliest calendar date from this GEDCOM date.
339a25f0a04SGreg Roach	 *
340a25f0a04SGreg Roach	 * In the date “FROM 1900 TO 1910”, this would be 1900.
341a25f0a04SGreg Roach	 *
342a25f0a04SGreg Roach	 * @return CalendarDate
343a25f0a04SGreg Roach	 */
344a25f0a04SGreg Roach	function MinDate() {
345a25f0a04SGreg Roach		return $this->date1;
346a25f0a04SGreg Roach	}
347a25f0a04SGreg Roach
348a25f0a04SGreg Roach	/**
349a25f0a04SGreg Roach	 * Get the latest calendar date from this GEDCOM date.
350a25f0a04SGreg Roach	 *
351a25f0a04SGreg Roach	 * In the date “FROM 1900 TO 1910”, this would be 1910.
352a25f0a04SGreg Roach	 *
353a25f0a04SGreg Roach	 * @return CalendarDate
354a25f0a04SGreg Roach	 */
355a25f0a04SGreg Roach	function MaxDate() {
356a25f0a04SGreg Roach		if (is_null($this->date2)) {
357a25f0a04SGreg Roach			return $this->date1;
358a25f0a04SGreg Roach		} else {
359a25f0a04SGreg Roach			return $this->date2;
360a25f0a04SGreg Roach		}
361a25f0a04SGreg Roach	}
362a25f0a04SGreg Roach
363a25f0a04SGreg Roach	/**
364a25f0a04SGreg Roach	 * Get the earliest Julian day number from this GEDCOM date.
365a25f0a04SGreg Roach	 *
366a25f0a04SGreg Roach	 * @return integer
367a25f0a04SGreg Roach	 */
368a25f0a04SGreg Roach	function MinJD() {
369a25f0a04SGreg Roach		$tmp = $this->MinDate();
370a25f0a04SGreg Roach
371a25f0a04SGreg Roach		return $tmp->minJD;
372a25f0a04SGreg Roach	}
373a25f0a04SGreg Roach
374a25f0a04SGreg Roach	/**
375a25f0a04SGreg Roach	 * Get the latest Julian day number from this GEDCOM date.
376a25f0a04SGreg Roach	 *
377a25f0a04SGreg Roach	 * @return integer
378a25f0a04SGreg Roach	 */
379a25f0a04SGreg Roach	function MaxJD() {
380a25f0a04SGreg Roach		$tmp = $this->MaxDate();
381a25f0a04SGreg Roach
382a25f0a04SGreg Roach		return $tmp->maxJD;
383a25f0a04SGreg Roach	}
384a25f0a04SGreg Roach
385a25f0a04SGreg Roach	/**
386a25f0a04SGreg Roach	 * Get the middle Julian day number from the GEDCOM date.
387a25f0a04SGreg Roach	 *
388a25f0a04SGreg Roach	 * For a month-only date, this would be somewhere around the 16 day.
389a25f0a04SGreg Roach	 * For a year-only date, this would be somewhere around 1st July.
390a25f0a04SGreg Roach	 *
391a25f0a04SGreg Roach	 * @return integer
392a25f0a04SGreg Roach	 */
393a25f0a04SGreg Roach	function JD() {
394a25f0a04SGreg Roach		return (int) (($this->MinJD() + $this->MaxJD()) / 2);
395a25f0a04SGreg Roach	}
396a25f0a04SGreg Roach
397a25f0a04SGreg Roach	/**
398a25f0a04SGreg Roach	 * Offset this date by N years, and round to the whole year.
399a25f0a04SGreg Roach	 *
400a25f0a04SGreg Roach	 * This is typically used to create an estimated death date,
401a25f0a04SGreg Roach	 * which is before a certain number of years after the birth date.
402a25f0a04SGreg Roach	 *
403a25f0a04SGreg Roach	 * @param integer $years     - a number of years, positive or negative
404a25f0a04SGreg Roach	 * @param string  $qualifier - typically “BEF” or “AFT”
405a25f0a04SGreg Roach	 *
406a25f0a04SGreg Roach	 * @return Date
407a25f0a04SGreg Roach	 */
408a25f0a04SGreg Roach	function AddYears($years, $qualifier = '') {
409a25f0a04SGreg Roach		$tmp = clone $this;
410a25f0a04SGreg Roach		$tmp->date1->y += $years;
411a25f0a04SGreg Roach		$tmp->date1->m = 0;
412a25f0a04SGreg Roach		$tmp->date1->d = 0;
413a25f0a04SGreg Roach		$tmp->date1->setJdFromYmd();
414a25f0a04SGreg Roach		$tmp->qual1 = $qualifier;
415a25f0a04SGreg Roach		$tmp->qual2 = '';
416a25f0a04SGreg Roach		$tmp->date2 = null;
417a25f0a04SGreg Roach
418a25f0a04SGreg Roach		return $tmp;
419a25f0a04SGreg Roach	}
420a25f0a04SGreg Roach
421a25f0a04SGreg Roach	/**
422a25f0a04SGreg Roach	 * Calculate the the age of a person, on a date.
423a25f0a04SGreg Roach	 *
424a25f0a04SGreg Roach	 * @param Date    $d1
425a25f0a04SGreg Roach	 * @param Date    $d2
426a25f0a04SGreg Roach	 * @param integer $format
427a25f0a04SGreg Roach	 *
428a25f0a04SGreg Roach	 * @return int|string
429a25f0a04SGreg Roach	 * @throws \InvalidArgumentException
430a25f0a04SGreg Roach	 */
431a25f0a04SGreg Roach	static function getAge(Date $d1, Date $d2 = null, $format = 0) {
432a25f0a04SGreg Roach		if ($d2) {
433a25f0a04SGreg Roach			if ($d2->MaxJD() >= $d1->MinJD() && $d2->MinJD() <= $d1->MinJD()) {
434a25f0a04SGreg Roach				// Overlapping dates
435a25f0a04SGreg Roach				$jd = $d1->MinJD();
436a25f0a04SGreg Roach			} else {
437a25f0a04SGreg Roach				// Non-overlapping dates
438a25f0a04SGreg Roach				$jd = $d2->MinJD();
439a25f0a04SGreg Roach			}
440a25f0a04SGreg Roach		} else {
441a25f0a04SGreg Roach			// If second date not specified, use today’s date
442a25f0a04SGreg Roach			$jd = WT_CLIENT_JD;
443a25f0a04SGreg Roach		}
444a25f0a04SGreg Roach
445a25f0a04SGreg Roach		switch ($format) {
446*5fec6c0aSGreg Roach		case 0:
447*5fec6c0aSGreg Roach			// Years - integer only (for statistics, rather than for display)
448a25f0a04SGreg Roach			if ($jd && $d1->MinJD() && $d1->MinJD() <= $jd) {
449a25f0a04SGreg Roach				return $d1->MinDate()->getAge(false, $jd, false);
450a25f0a04SGreg Roach			} else {
451a25f0a04SGreg Roach				return -1;
452a25f0a04SGreg Roach			}
453*5fec6c0aSGreg Roach		case 1:
454*5fec6c0aSGreg Roach			// Days - integer only (for sorting, rather than for display)
455a25f0a04SGreg Roach			if ($jd && $d1->MinJD()) {
456a25f0a04SGreg Roach				return $jd - $d1->MinJD();
457a25f0a04SGreg Roach			} else {
458a25f0a04SGreg Roach				return -1;
459a25f0a04SGreg Roach			}
460*5fec6c0aSGreg Roach		case 2:
461*5fec6c0aSGreg Roach			// Just years, in local digits, with warning for negative/
462a25f0a04SGreg Roach			if ($jd && $d1->MinJD()) {
463a25f0a04SGreg Roach				if ($d1->MinJD() > $jd) {
464a25f0a04SGreg Roach					return '<i class="icon-warning"></i>';
465a25f0a04SGreg Roach				} else {
466a25f0a04SGreg Roach					return I18N::number($d1->MinDate()->getAge(false, $jd));
467a25f0a04SGreg Roach				}
468a25f0a04SGreg Roach			} else {
469a25f0a04SGreg Roach				return '&nbsp;';
470a25f0a04SGreg Roach			}
471a25f0a04SGreg Roach		default:
472a25f0a04SGreg Roach			throw new \InvalidArgumentException('format: ' . $format);
473a25f0a04SGreg Roach		}
474a25f0a04SGreg Roach	}
475a25f0a04SGreg Roach
476a25f0a04SGreg Roach	/**
477a25f0a04SGreg Roach	 * Calculate the years/months/days between two events
478a25f0a04SGreg Roach	 * Return a gedcom style age string: "1y 2m 3d" (for fact details)
479a25f0a04SGreg Roach	 *
480a25f0a04SGreg Roach	 * @param Date      $d1
481a25f0a04SGreg Roach	 * @param Date|null $d2
482a25f0a04SGreg Roach	 * @param boolean      $warn_on_negative
483a25f0a04SGreg Roach	 *
484a25f0a04SGreg Roach	 * @return string
485a25f0a04SGreg Roach	 */
486a25f0a04SGreg Roach	static function GetAgeGedcom(Date $d1, Date $d2 = null, $warn_on_negative = true) {
487a25f0a04SGreg Roach		if (is_null($d2)) {
488a25f0a04SGreg Roach			return $d1->date1->GetAge(true, WT_CLIENT_JD, $warn_on_negative);
489a25f0a04SGreg Roach		} else {
490a25f0a04SGreg Roach			// If dates overlap, then can’t calculate age.
491a25f0a04SGreg Roach			if (self::Compare($d1, $d2)) {
492a25f0a04SGreg Roach				return $d1->date1->GetAge(true, $d2->MinJD(), $warn_on_negative);
493a25f0a04SGreg Roach			} elseif (self::Compare($d1, $d2) == 0 && $d1->date1->minJD == $d2->MinJD()) {
494a25f0a04SGreg Roach				return '0d';
495a25f0a04SGreg Roach			} else {
496a25f0a04SGreg Roach				return '';
497a25f0a04SGreg Roach			}
498a25f0a04SGreg Roach		}
499a25f0a04SGreg Roach	}
500a25f0a04SGreg Roach
501a25f0a04SGreg Roach	/**
502a25f0a04SGreg Roach	 * Compare two dates, so they can be sorted.
503a25f0a04SGreg Roach	 *
504a25f0a04SGreg Roach	 * return <0 if $a<$b
505a25f0a04SGreg Roach	 * return >0 if $b>$a
506a25f0a04SGreg Roach	 * return  0 if dates same/overlap
507a25f0a04SGreg Roach	 * BEF/AFT sort as the day before/after
508a25f0a04SGreg Roach	 *
509a25f0a04SGreg Roach	 * @param Date $a
510a25f0a04SGreg Roach	 * @param Date $b
511a25f0a04SGreg Roach	 *
512a25f0a04SGreg Roach	 * @return integer
513a25f0a04SGreg Roach	 */
514a25f0a04SGreg Roach	static function Compare(Date $a, Date $b) {
515a25f0a04SGreg Roach		// Get min/max JD for each date.
516a25f0a04SGreg Roach		switch ($a->qual1) {
517a25f0a04SGreg Roach		case 'BEF':
518a25f0a04SGreg Roach			$amin = $a->MinJD() - 1;
519a25f0a04SGreg Roach			$amax = $amin;
520a25f0a04SGreg Roach			break;
521a25f0a04SGreg Roach		case 'AFT':
522a25f0a04SGreg Roach			$amax = $a->MaxJD() + 1;
523a25f0a04SGreg Roach			$amin = $amax;
524a25f0a04SGreg Roach			break;
525a25f0a04SGreg Roach		default:
526a25f0a04SGreg Roach			$amin = $a->MinJD();
527a25f0a04SGreg Roach			$amax = $a->MaxJD();
528a25f0a04SGreg Roach			break;
529a25f0a04SGreg Roach		}
530a25f0a04SGreg Roach		switch ($b->qual1) {
531a25f0a04SGreg Roach		case 'BEF':
532a25f0a04SGreg Roach			$bmin = $b->MinJD() - 1;
533a25f0a04SGreg Roach			$bmax = $bmin;
534a25f0a04SGreg Roach			break;
535a25f0a04SGreg Roach		case 'AFT':
536a25f0a04SGreg Roach			$bmax = $b->MaxJD() + 1;
537a25f0a04SGreg Roach			$bmin = $bmax;
538a25f0a04SGreg Roach			break;
539a25f0a04SGreg Roach		default:
540a25f0a04SGreg Roach			$bmin = $b->MinJD();
541a25f0a04SGreg Roach			$bmax = $b->MaxJD();
542a25f0a04SGreg Roach			break;
543a25f0a04SGreg Roach		}
544a25f0a04SGreg Roach		if ($amax < $bmin) {
545a25f0a04SGreg Roach			return -1;
546a25f0a04SGreg Roach		} elseif ($amin > $bmax && $bmax > 0) {
547a25f0a04SGreg Roach			return 1;
548a25f0a04SGreg Roach		} elseif ($amin < $bmin && $amax <= $bmax) {
549a25f0a04SGreg Roach			return -1;
550a25f0a04SGreg Roach		} elseif ($amin > $bmin && $amax >= $bmax && $bmax > 0) {
551a25f0a04SGreg Roach			return 1;
552a25f0a04SGreg Roach		} else {
553a25f0a04SGreg Roach			return 0;
554a25f0a04SGreg Roach		}
555a25f0a04SGreg Roach	}
556a25f0a04SGreg Roach
557a25f0a04SGreg Roach	/**
558a25f0a04SGreg Roach	 * Check whether a gedcom date contains usable calendar date(s).
559a25f0a04SGreg Roach	 *
560a25f0a04SGreg Roach	 * An incomplete date such as "12 AUG" would be invalid, as
561a25f0a04SGreg Roach	 * we cannot sort it.
562a25f0a04SGreg Roach	 *
563a25f0a04SGreg Roach	 * @return boolean
564a25f0a04SGreg Roach	 */
565a25f0a04SGreg Roach	function isOK() {
566a25f0a04SGreg Roach		return $this->MinJD() && $this->MaxJD();
567a25f0a04SGreg Roach	}
568a25f0a04SGreg Roach
569a25f0a04SGreg Roach	/**
570a25f0a04SGreg Roach	 * Calculate the gregorian year for a date.  This should NOT be used internally
571a25f0a04SGreg Roach	 * within WT - we should keep the code "calendar neutral" to allow support for
572a25f0a04SGreg Roach	 * jewish/arabic users.  This is only for interfacing with external entities,
573a25f0a04SGreg Roach	 * such as the ancestry.com search interface or the dated fact icons.
574a25f0a04SGreg Roach	 *
575a25f0a04SGreg Roach	 * @return integer
576a25f0a04SGreg Roach	 */
577a25f0a04SGreg Roach	function gregorianYear() {
578a25f0a04SGreg Roach		if ($this->isOK()) {
579a25f0a04SGreg Roach			$gregorian_calendar = new GregorianCalendar;
580a25f0a04SGreg Roach			list($year) = $gregorian_calendar->jdToYmd($this->JD());
581a25f0a04SGreg Roach
582a25f0a04SGreg Roach			return $year;
583a25f0a04SGreg Roach		} else {
584a25f0a04SGreg Roach			return 0;
585a25f0a04SGreg Roach		}
586a25f0a04SGreg Roach	}
587a25f0a04SGreg Roach}
588