1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Date; 21 22use Fisharebest\ExtCalendar\CalendarInterface; 23use Fisharebest\ExtCalendar\JewishCalendar; 24use Fisharebest\Webtrees\Carbon; 25use Fisharebest\Webtrees\Http\RequestHandlers\CalendarPage; 26use Fisharebest\Webtrees\I18N; 27use Fisharebest\Webtrees\Tree; 28use InvalidArgumentException; 29 30use function array_key_exists; 31use function array_search; 32use function get_class; 33use function intdiv; 34use function is_array; 35use function is_int; 36use function max; 37use function preg_match; 38use function route; 39use function sprintf; 40use function str_contains; 41use function strpbrk; 42use function strtr; 43use function trigger_error; 44use function trim; 45use function view; 46 47use const E_USER_DEPRECATED; 48 49/** 50 * Classes for Gedcom Date/Calendar functionality. 51 * 52 * CalendarDate is a base class for classes such as GregorianDate, etc. 53 * + All supported calendars have non-zero days/months/years. 54 * + We store dates as both Y/M/D and Julian Days. 55 * + For imprecise dates such as "JAN 2000" we store the start/end julian day. 56 * 57 * NOTE: Since different calendars start their days at different times, (civil 58 * midnight, solar midnight, sunset, sunrise, etc.), we convert on the basis of 59 * midday. 60 */ 61abstract class AbstractCalendarDate 62{ 63 // GEDCOM calendar escape 64 public const ESCAPE = '@#DUNKNOWN@'; 65 66 // Convert GEDCOM month names to month numbers. 67 protected const MONTH_ABBREVIATIONS = []; 68 69 /** @var CalendarInterface The calendar system used to represent this date */ 70 protected $calendar; 71 72 /** @var int Year number */ 73 public $year; 74 75 /** @var int Month number */ 76 public $month; 77 78 /** @var int Day number */ 79 public $day; 80 81 /** @var int Earliest Julian day number (start of month/year for imprecise dates) */ 82 private $minimum_julian_day; 83 84 /** @var int Latest Julian day number (end of month/year for imprecise dates) */ 85 private $maximum_julian_day; 86 87 /** 88 * Create a date from either: 89 * a Julian day number 90 * day/month/year strings from a GEDCOM date 91 * another CalendarDate object 92 * 93 * @param array<string>|int|AbstractCalendarDate $date 94 */ 95 protected function __construct($date) 96 { 97 // Construct from an integer (a julian day number) 98 if (is_int($date)) { 99 $this->minimum_julian_day = $date; 100 $this->maximum_julian_day = $date; 101 [$this->year, $this->month, $this->day] = $this->calendar->jdToYmd($date); 102 103 return; 104 } 105 106 // Construct from an array (of three gedcom-style strings: "1900", "FEB", "4") 107 if (is_array($date)) { 108 $this->day = (int) $date[2]; 109 if (array_key_exists($date[1], static::MONTH_ABBREVIATIONS)) { 110 $this->month = static::MONTH_ABBREVIATIONS[$date[1]]; 111 } else { 112 $this->month = 0; 113 $this->day = 0; 114 } 115 $this->year = $this->extractYear($date[0]); 116 117 // Our simple lookup table above does not take into account Adar and leap-years. 118 if ($this->month === 6 && $this->calendar instanceof JewishCalendar && !$this->calendar->isLeapYear($this->year)) { 119 $this->month = 7; 120 } 121 122 $this->setJdFromYmd(); 123 124 return; 125 } 126 127 // Construct from a CalendarDate 128 $this->minimum_julian_day = $date->minimum_julian_day; 129 $this->maximum_julian_day = $date->maximum_julian_day; 130 131 // Construct from an equivalent xxxxDate object 132 if (get_class($this) === get_class($date)) { 133 $this->year = $date->year; 134 $this->month = $date->month; 135 $this->day = $date->day; 136 137 return; 138 } 139 140 // Not all dates can be converted 141 if (!$this->inValidRange()) { 142 $this->year = 0; 143 $this->month = 0; 144 $this->day = 0; 145 146 return; 147 } 148 149 // ...else construct an inequivalent xxxxDate object 150 if ($date->year === 0) { 151 // Incomplete date - convert on basis of anniversary in current year 152 $today = $date->calendar->jdToYmd(Carbon::now()->julianDay()); 153 $jd = $date->calendar->ymdToJd($today[0], $date->month, $date->day === 0 ? $today[2] : $date->day); 154 } else { 155 // Complete date 156 $jd = intdiv($date->maximum_julian_day + $date->minimum_julian_day, 2); 157 } 158 [$this->year, $this->month, $this->day] = $this->calendar->jdToYmd($jd); 159 // New date has same precision as original date 160 if ($date->year === 0) { 161 $this->year = 0; 162 } 163 if ($date->month === 0) { 164 $this->month = 0; 165 } 166 if ($date->day === 0) { 167 $this->day = 0; 168 } 169 $this->setJdFromYmd(); 170 } 171 172 /** 173 * @return int 174 */ 175 public function maximumJulianDay(): int 176 { 177 return $this->maximum_julian_day; 178 } 179 180 /** 181 * @return int 182 */ 183 public function year(): int 184 { 185 return $this->year; 186 } 187 188 /** 189 * @return int 190 */ 191 public function month(): int 192 { 193 return $this->month; 194 } 195 196 /** 197 * @return int 198 */ 199 public function day(): int 200 { 201 return $this->day; 202 } 203 204 /** 205 * @return int 206 */ 207 public function minimumJulianDay(): int 208 { 209 return $this->minimum_julian_day; 210 } 211 212 /** 213 * Is the current year a leap year? 214 * 215 * @return bool 216 */ 217 public function isLeapYear(): bool 218 { 219 return $this->calendar->isLeapYear($this->year); 220 } 221 222 /** 223 * Set the object’s Julian day number from a potentially incomplete year/month/day 224 * 225 * @return void 226 */ 227 public function setJdFromYmd(): void 228 { 229 if ($this->year === 0) { 230 $this->minimum_julian_day = 0; 231 $this->maximum_julian_day = 0; 232 } elseif ($this->month === 0) { 233 $this->minimum_julian_day = $this->calendar->ymdToJd($this->year, 1, 1); 234 $this->maximum_julian_day = $this->calendar->ymdToJd($this->nextYear($this->year), 1, 1) - 1; 235 } elseif ($this->day === 0) { 236 [$ny, $nm] = $this->nextMonth(); 237 $this->minimum_julian_day = $this->calendar->ymdToJd($this->year, $this->month, 1); 238 $this->maximum_julian_day = $this->calendar->ymdToJd($ny, $nm, 1) - 1; 239 } else { 240 $this->minimum_julian_day = $this->calendar->ymdToJd($this->year, $this->month, $this->day); 241 $this->maximum_julian_day = $this->minimum_julian_day; 242 } 243 } 244 245 /** 246 * Full day of the week 247 * 248 * @param int $day_number 249 * 250 * @return string 251 */ 252 public function dayNames(int $day_number): string 253 { 254 static $translated_day_names; 255 256 if ($translated_day_names === null) { 257 $translated_day_names = [ 258 0 => I18N::translate('Monday'), 259 1 => I18N::translate('Tuesday'), 260 2 => I18N::translate('Wednesday'), 261 3 => I18N::translate('Thursday'), 262 4 => I18N::translate('Friday'), 263 5 => I18N::translate('Saturday'), 264 6 => I18N::translate('Sunday'), 265 ]; 266 } 267 268 return $translated_day_names[$day_number]; 269 } 270 271 /** 272 * Abbreviated day of the week 273 * 274 * @param int $day_number 275 * 276 * @return string 277 */ 278 protected function dayNamesAbbreviated(int $day_number): string 279 { 280 static $translated_day_names; 281 282 if ($translated_day_names === null) { 283 $translated_day_names = [ 284 /* I18N: abbreviation for Monday */ 285 0 => I18N::translate('Mon'), 286 /* I18N: abbreviation for Tuesday */ 287 1 => I18N::translate('Tue'), 288 /* I18N: abbreviation for Wednesday */ 289 2 => I18N::translate('Wed'), 290 /* I18N: abbreviation for Thursday */ 291 3 => I18N::translate('Thu'), 292 /* I18N: abbreviation for Friday */ 293 4 => I18N::translate('Fri'), 294 /* I18N: abbreviation for Saturday */ 295 5 => I18N::translate('Sat'), 296 /* I18N: abbreviation for Sunday */ 297 6 => I18N::translate('Sun'), 298 ]; 299 } 300 301 return $translated_day_names[$day_number]; 302 } 303 304 /** 305 * Most years are 1 more than the previous, but not always (e.g. 1BC->1AD) 306 * 307 * @param int $year 308 * 309 * @return int 310 */ 311 protected function nextYear(int $year): int 312 { 313 return $year + 1; 314 } 315 316 /** 317 * Calendars that use suffixes, etc. (e.g. “B.C.”) or OS/NS notation should redefine this. 318 * 319 * @param string $year 320 * 321 * @return int 322 */ 323 protected function extractYear(string $year): int 324 { 325 return (int) $year; 326 } 327 328 /** 329 * Compare two dates, for sorting 330 * 331 * @param AbstractCalendarDate $d1 332 * @param AbstractCalendarDate $d2 333 * 334 * @return int 335 */ 336 public static function compare(AbstractCalendarDate $d1, AbstractCalendarDate $d2): int 337 { 338 if ($d1->maximum_julian_day < $d2->minimum_julian_day) { 339 return -1; 340 } 341 342 if ($d2->maximum_julian_day < $d1->minimum_julian_day) { 343 return 1; 344 } 345 346 return 0; 347 } 348 349 /** 350 * Calculate the years/months/days between this date and another date. 351 * Results assume you add the days first, then the months. 352 * 4 February -> 3 July is 27 days (3 March) and 4 months. 353 * It is not 4 months (4 June) and 29 days. 354 * 355 * @param AbstractCalendarDate $date 356 * 357 * @return int[] Age in years/months/days 358 */ 359 public function ageDifference(AbstractCalendarDate $date): array 360 { 361 // Incomplete dates 362 if ($this->year === 0 || $date->year === 0) { 363 return [-1, -1, -1]; 364 } 365 366 // Overlapping dates 367 if (self::compare($this, $date) === 0) { 368 return [0, 0, 0]; 369 } 370 371 // Perform all calculations using the calendar of the first date 372 [$year1, $month1, $day1] = $this->calendar->jdToYmd($this->minimum_julian_day); 373 [$year2, $month2, $day2] = $this->calendar->jdToYmd($date->minimum_julian_day); 374 375 $years = $year2 - $year1; 376 $months = $month2 - $month1; 377 $days = $day2 - $day1; 378 379 if ($days < 0) { 380 $days += $this->calendar->daysInMonth($year1, $month1); 381 $months--; 382 } 383 384 if ($months < 0) { 385 $months += $this->calendar->monthsInYear($year2); 386 $years--; 387 } 388 389 return [$years, $months, $days]; 390 } 391 392 /** 393 * Convert a date from one calendar to another. 394 * 395 * @param string $calendar 396 * 397 * @return AbstractCalendarDate 398 */ 399 public function convertToCalendar(string $calendar): AbstractCalendarDate 400 { 401 switch ($calendar) { 402 case 'gregorian': 403 return new GregorianDate($this); 404 case 'julian': 405 return new JulianDate($this); 406 case 'jewish': 407 return new JewishDate($this); 408 case 'french': 409 return new FrenchDate($this); 410 case 'hijri': 411 return new HijriDate($this); 412 case 'jalali': 413 return new JalaliDate($this); 414 default: 415 return $this; 416 } 417 } 418 419 /** 420 * Is this date within the valid range of the calendar 421 * 422 * @return bool 423 */ 424 public function inValidRange(): bool 425 { 426 return $this->minimum_julian_day >= $this->calendar->jdStart() && $this->maximum_julian_day <= $this->calendar->jdEnd(); 427 } 428 429 /** 430 * How many months in a year 431 * 432 * @return int 433 */ 434 public function monthsInYear(): int 435 { 436 return $this->calendar->monthsInYear(); 437 } 438 439 /** 440 * How many days in the current month 441 * 442 * @return int 443 */ 444 public function daysInMonth(): int 445 { 446 try { 447 return $this->calendar->daysInMonth($this->year, $this->month); 448 } catch (InvalidArgumentException $ex) { 449 // calendar.php calls this with "DD MMM" dates, for which we cannot calculate 450 // the length of a month. Should we validate this before calling this function? 451 return 0; 452 } 453 } 454 455 /** 456 * How many days in the current week 457 * 458 * @return int 459 */ 460 public function daysInWeek(): int 461 { 462 return $this->calendar->daysInWeek(); 463 } 464 465 /** 466 * Format a date, using similar codes to the PHP date() function. 467 * 468 * @param string $format See http://php.net/date 469 * @param string $qualifier GEDCOM qualifier, so we can choose the right case for the month name. 470 * 471 * @return string 472 */ 473 public function format(string $format, string $qualifier = ''): string 474 { 475 // Dates can include additional punctuation and symbols. e.g. 476 // %F %j, %Y 477 // %Y. %F %d. 478 // %Y年 %n月 %j日 479 // %j. %F %Y 480 // Don’t show exact details or unnecessary punctuation for inexact dates. 481 if ($this->day === 0) { 482 $format = strtr($format, ['%d' => '', '%j日' => '', '%j,' => '', '%j' => '', '%l' => '', '%D' => '', '%N' => '', '%S' => '', '%w' => '', '%z' => '']); 483 } 484 if ($this->month === 0) { 485 $format = strtr($format, ['%F' => '', '%m' => '', '%M' => '', '年 %n月' => '', '%n' => '', '%t' => '']); 486 } 487 if ($this->year === 0) { 488 $format = strtr($format, ['%t' => '', '%L' => '', '%G' => '', '%y' => '', '%Y年' => '', '%Y' => '']); 489 } 490 $format = trim($format, ',. /-'); 491 492 if ($this->day !== 0 && preg_match('/%[djlDNSwz]/', $format)) { 493 // If we have a day-number *and* we are being asked to display it, then genitive 494 $case = 'GENITIVE'; 495 } else { 496 switch ($qualifier) { 497 case 'TO': 498 case 'ABT': 499 case 'FROM': 500 $case = 'GENITIVE'; 501 break; 502 case 'AFT': 503 $case = 'LOCATIVE'; 504 break; 505 case 'BEF': 506 case 'BET': 507 case 'AND': 508 $case = 'INSTRUMENTAL'; 509 break; 510 case '': 511 case 'INT': 512 case 'EST': 513 case 'CAL': 514 default: // There shouldn't be any other options... 515 $case = 'NOMINATIVE'; 516 break; 517 } 518 } 519 // Build up the formatted date, character at a time 520 if (str_contains($format, '%d')) { 521 $format = strtr($format, ['%d' => $this->formatDayZeros()]); 522 } 523 if (str_contains($format, '%j')) { 524 $format = strtr($format, ['%j' => $this->formatDay()]); 525 } 526 if (str_contains($format, '%l')) { 527 $format = strtr($format, ['%l' => $this->formatLongWeekday()]); 528 } 529 if (str_contains($format, '%D')) { 530 $format = strtr($format, ['%D' => $this->formatShortWeekday()]); 531 } 532 if (str_contains($format, '%N')) { 533 $format = strtr($format, ['%N' => $this->formatIsoWeekday()]); 534 } 535 if (str_contains($format, '%w')) { 536 $format = strtr($format, ['%w' => $this->formatNumericWeekday()]); 537 } 538 if (str_contains($format, '%z')) { 539 $format = strtr($format, ['%z' => $this->formatDayOfYear()]); 540 } 541 if (str_contains($format, '%F')) { 542 $format = strtr($format, ['%F' => $this->formatLongMonth($case)]); 543 } 544 if (str_contains($format, '%m')) { 545 $format = strtr($format, ['%m' => $this->formatMonthZeros()]); 546 } 547 if (str_contains($format, '%M')) { 548 $format = strtr($format, ['%M' => $this->formatShortMonth()]); 549 } 550 if (str_contains($format, '%n')) { 551 $format = strtr($format, ['%n' => $this->formatMonth()]); 552 } 553 if (str_contains($format, '%t')) { 554 $format = strtr($format, ['%t' => (string) $this->daysInMonth()]); 555 } 556 if (str_contains($format, '%L')) { 557 $format = strtr($format, ['%L' => $this->isLeapYear() ? '1' : '0']); 558 } 559 if (str_contains($format, '%Y')) { 560 $format = strtr($format, ['%Y' => $this->formatLongYear()]); 561 } 562 if (str_contains($format, '%y')) { 563 $format = strtr($format, ['%y' => $this->formatShortYear()]); 564 } 565 // These 4 extensions are useful for re-formatting gedcom dates. 566 if (str_contains($format, '%@')) { 567 $format = strtr($format, ['%@' => $this->formatGedcomCalendarEscape()]); 568 } 569 if (str_contains($format, '%A')) { 570 $format = strtr($format, ['%A' => $this->formatGedcomDay()]); 571 } 572 if (str_contains($format, '%O')) { 573 $format = strtr($format, ['%O' => $this->formatGedcomMonth()]); 574 } 575 if (str_contains($format, '%E')) { 576 $format = strtr($format, ['%E' => $this->formatGedcomYear()]); 577 } 578 579 return $format; 580 } 581 582 /** 583 * Generate the %d format for a date. 584 * 585 * @return string 586 */ 587 protected function formatDayZeros(): string 588 { 589 if ($this->day > 9) { 590 return I18N::digits($this->day); 591 } 592 593 return I18N::digits('0' . $this->day); 594 } 595 596 /** 597 * Generate the %j format for a date. 598 * 599 * @return string 600 */ 601 protected function formatDay(): string 602 { 603 return I18N::digits($this->day); 604 } 605 606 /** 607 * Generate the %l format for a date. 608 * 609 * @return string 610 */ 611 protected function formatLongWeekday(): string 612 { 613 return $this->dayNames($this->minimum_julian_day % $this->calendar->daysInWeek()); 614 } 615 616 /** 617 * Generate the %D format for a date. 618 * 619 * @return string 620 */ 621 protected function formatShortWeekday(): string 622 { 623 return $this->dayNamesAbbreviated($this->minimum_julian_day % $this->calendar->daysInWeek()); 624 } 625 626 /** 627 * Generate the %N format for a date. 628 * 629 * @return string 630 */ 631 protected function formatIsoWeekday(): string 632 { 633 return I18N::digits($this->minimum_julian_day % 7 + 1); 634 } 635 636 /** 637 * Generate the %w format for a date. 638 * 639 * @return string 640 */ 641 protected function formatNumericWeekday(): string 642 { 643 return I18N::digits(($this->minimum_julian_day + 1) % $this->calendar->daysInWeek()); 644 } 645 646 /** 647 * Generate the %z format for a date. 648 * 649 * @return string 650 */ 651 protected function formatDayOfYear(): string 652 { 653 return I18N::digits($this->minimum_julian_day - $this->calendar->ymdToJd($this->year, 1, 1)); 654 } 655 656 /** 657 * Generate the %n format for a date. 658 * 659 * @return string 660 */ 661 protected function formatMonth(): string 662 { 663 return I18N::digits($this->month); 664 } 665 666 /** 667 * Generate the %m format for a date. 668 * 669 * @return string 670 */ 671 protected function formatMonthZeros(): string 672 { 673 if ($this->month > 9) { 674 return I18N::digits($this->month); 675 } 676 677 return I18N::digits('0' . $this->month); 678 } 679 680 /** 681 * Generate the %F format for a date. 682 * 683 * @param string $case Which grammatical case shall we use 684 * 685 * @return string 686 */ 687 protected function formatLongMonth($case = 'NOMINATIVE'): string 688 { 689 switch ($case) { 690 case 'GENITIVE': 691 return $this->monthNameGenitiveCase($this->month, $this->isLeapYear()); 692 case 'NOMINATIVE': 693 return $this->monthNameNominativeCase($this->month, $this->isLeapYear()); 694 case 'LOCATIVE': 695 return $this->monthNameLocativeCase($this->month, $this->isLeapYear()); 696 case 'INSTRUMENTAL': 697 return $this->monthNameInstrumentalCase($this->month, $this->isLeapYear()); 698 default: 699 throw new InvalidArgumentException($case); 700 } 701 } 702 703 /** 704 * Full month name in genitive case. 705 * 706 * @param int $month 707 * @param bool $leap_year Some calendars use leap months 708 * 709 * @return string 710 */ 711 abstract protected function monthNameGenitiveCase(int $month, bool $leap_year): string; 712 713 /** 714 * Full month name in nominative case. 715 * 716 * @param int $month 717 * @param bool $leap_year Some calendars use leap months 718 * 719 * @return string 720 */ 721 abstract protected function monthNameNominativeCase(int $month, bool $leap_year): string; 722 723 /** 724 * Full month name in locative case. 725 * 726 * @param int $month 727 * @param bool $leap_year Some calendars use leap months 728 * 729 * @return string 730 */ 731 abstract protected function monthNameLocativeCase(int $month, bool $leap_year): string; 732 733 /** 734 * Full month name in instrumental case. 735 * 736 * @param int $month 737 * @param bool $leap_year Some calendars use leap months 738 * 739 * @return string 740 */ 741 abstract protected function monthNameInstrumentalCase(int $month, bool $leap_year): string; 742 743 /** 744 * Abbreviated month name 745 * 746 * @param int $month 747 * @param bool $leap_year Some calendars use leap months 748 * 749 * @return string 750 */ 751 abstract protected function monthNameAbbreviated(int $month, bool $leap_year): string; 752 753 /** 754 * Generate the %M format for a date. 755 * 756 * @return string 757 */ 758 protected function formatShortMonth(): string 759 { 760 return $this->monthNameAbbreviated($this->month, $this->isLeapYear()); 761 } 762 763 /** 764 * Generate the %y format for a date. 765 * NOTE Short year is NOT a 2-digit year. It is for calendars such as hebrew 766 * which have a 3-digit form of 4-digit years. 767 * 768 * @return string 769 */ 770 protected function formatShortYear(): string 771 { 772 return $this->formatLongYear(); 773 } 774 775 /** 776 * Generate the %A format for a date. 777 * 778 * @return string 779 */ 780 protected function formatGedcomDay(): string 781 { 782 if ($this->day === 0) { 783 return ''; 784 } 785 786 return sprintf('%02d', $this->day); 787 } 788 789 /** 790 * Generate the %O format for a date. 791 * 792 * @return string 793 */ 794 protected function formatGedcomMonth(): string 795 { 796 // Our simple lookup table doesn't work correctly for Adar on leap years 797 if ($this->month === 7 && $this->calendar instanceof JewishCalendar && !$this->calendar->isLeapYear($this->year)) { 798 return 'ADR'; 799 } 800 801 return array_search($this->month, static::MONTH_ABBREVIATIONS, true); 802 } 803 804 /** 805 * Generate the %E format for a date. 806 * 807 * @return string 808 */ 809 protected function formatGedcomYear(): string 810 { 811 if ($this->year === 0) { 812 return ''; 813 } 814 815 return sprintf('%04d', $this->year); 816 } 817 818 /** 819 * Generate the %@ format for a calendar escape. 820 * 821 * @return string 822 */ 823 protected function formatGedcomCalendarEscape(): string 824 { 825 return static::ESCAPE; 826 } 827 828 /** 829 * Generate the %Y format for a date. 830 * 831 * @return string 832 */ 833 protected function formatLongYear(): string 834 { 835 return I18N::digits($this->year); 836 } 837 838 /** 839 * Which months follows this one? Calendars with leap-months should provide their own implementation. 840 * 841 * @return int[] 842 */ 843 protected function nextMonth(): array 844 { 845 return [ 846 $this->month === $this->calendar->monthsInYear() ? $this->nextYear($this->year) : $this->year, 847 $this->month % $this->calendar->monthsInYear() + 1, 848 ]; 849 } 850 851 /** 852 * Get today’s date in the current calendar. 853 * 854 * @return int[] 855 */ 856 public function todayYmd(): array 857 { 858 return $this->calendar->jdToYmd(Carbon::now()->julianDay()); 859 } 860 861 /** 862 * Convert to today’s date. 863 * 864 * @return AbstractCalendarDate 865 */ 866 public function today(): AbstractCalendarDate 867 { 868 $tmp = clone $this; 869 $ymd = $tmp->todayYmd(); 870 $tmp->year = $ymd[0]; 871 $tmp->month = $ymd[1]; 872 $tmp->day = $ymd[2]; 873 $tmp->setJdFromYmd(); 874 875 return $tmp; 876 } 877 878 /** 879 * Create a URL that links this date to the WT calendar 880 * 881 * @param string $date_format 882 * @param Tree $tree 883 * 884 * @return string 885 */ 886 public function calendarUrl(string $date_format, Tree $tree): string 887 { 888 if ($this->day !== 0 && strpbrk($date_format, 'dDj')) { 889 // If the format includes a day, and the date also includes a day, then use the day view 890 $view = 'day'; 891 } elseif ($this->month !== 0 && strpbrk($date_format, 'FMmn')) { 892 // If the format includes a month, and the date also includes a month, then use the month view 893 $view = 'month'; 894 } else { 895 // Use the year view 896 $view = 'year'; 897 } 898 899 return route(CalendarPage::class, [ 900 'cal' => $this->calendar->gedcomCalendarEscape(), 901 'year' => $this->formatGedcomYear(), 902 'month' => $this->formatGedcomMonth(), 903 'day' => $this->formatGedcomDay(), 904 'view' => $view, 905 'tree' => $tree->name(), 906 ]); 907 } 908} 909