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