date-utilities.php 26 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
<?php
/**
 * Date Utilities file
 *
 * @category Utilities
 * @package  My Calendar
 * @author   Joe Dolson
 * @license  GPLv2 or later
 * @link     https://www.joedolson.com/my-calendar/
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Generate classes for a given date
 *
 * @param int $current timestamp.
 *
 * @return string classes
 */
function mc_dateclass( $current ) {
	$dayclass = sanitize_html_class( strtolower( date_i18n( 'l', $current ) ) ) . ' ' . sanitize_html_class( strtolower( date_i18n( 'D', $current ) ) );
	if ( current_time( 'Ymd' ) === mc_date( 'Ymd', $current, false ) ) {
		$dateclass = 'current-day';
	} elseif ( my_calendar_date_comp( current_time( 'Y-m-d' ), mc_date( 'Y-m-d', $current, false ) ) ) {
		$dateclass = 'future-day';
	} else {
		$dateclass = 'past-day past-date'; // stupid legacy classes.
	}

	return esc_attr( $dayclass . ' ' . $dateclass );
}

/**
 * Given a date and a quantity of time to add, produce new date
 *
 * @param string $givendate A time string.
 * @param int    $day Number of days to add.
 * @param int    $mth Number of months to add.
 * @param int    $yr number of years to add.
 *
 * @return int
 */
function my_calendar_add_date( $givendate, $day = 0, $mth = 0, $yr = 0 ) {
	$cd      = strtotime( $givendate );
	$newdate = mktime( mc_date( 'H', $cd, false ), mc_date( 'i', $cd, false ), mc_date( 's', $cd, false ), (int) mc_date( 'm', $cd, false ) + $mth, (int) mc_date( 'd', $cd, false ) + $day, (int) mc_date( 'Y', $cd, false ) + $yr );

	return $newdate;
}

/**
 * Test if the date is before or equal to second date with time precision
 *
 * @param string $early date string.
 * @param string $late date string.
 *
 * @return boolean true if first date earlier or equal
 */
function my_calendar_date_comp( $early, $late ) {
	$firstdate = strtotime( $early );
	$lastdate  = strtotime( $late );
	if ( $firstdate <= $lastdate ) {

		return true;
	} else {

		return false;
	}
}

/**
 * Test if first date before second date with time precision
 *
 * @param string $early date string.
 * @param string $late date string.
 *
 * @return boolean true if first date earlier
 */
function my_calendar_date_xcomp( $early, $late ) {
	$firstdate = strtotime( $early );
	$lastdate  = strtotime( $late );
	if ( $firstdate < $lastdate ) {

		return true;
	} else {

		return false;
	}
}

/**
 *  Test if dates are the same with day precision
 *
 * @param string $early date string in current time zone.
 * @param string $late date string in current time zone.
 *
 * @return boolean true if first date equal to second
 */
function my_calendar_date_equal( $early, $late ) {
	// convert full datetime to date only.
	$firstdate = strtotime( mc_date( 'Y-m-d', strtotime( $early ), false ) );
	$lastdate  = strtotime( mc_date( 'Y-m-d', strtotime( $late ), false ) );
	if ( $firstdate === $lastdate ) {

		return true;
	} else {

		return false;
	}
}

/**
 * Function to compare time in event objects for sorting
 *
 * @param object $a event object.
 * @param object $b event object.
 *
 * @return int (ternary value)
 */
function mc_time_cmp( $a, $b ) {
	if ( $a->occur_begin === $b->occur_begin ) {

		return 0;
	}

	return ( $a->occur_begin < $b->occur_begin ) ? - 1 : 1;
}

/**
 * Function to compare datetime in event objects & sort by string
 *
 * @param object $a event object.
 * @param object $b event object.
 *
 * @return integer (ternary value)
 */
function mc_datetime_cmp( $a, $b ) {
	$event_dt_a = strtotime( $a->occur_begin );
	$event_dt_b = strtotime( $b->occur_begin );
	if ( $event_dt_a === $event_dt_b ) {
		// this should sub-sort by title if date is the same. But it doesn't seem to.
		$ta = $a->event_title;
		$tb = $b->event_title;

		return strcmp( $ta, $tb );
	}

	return ( $event_dt_a < $event_dt_b ) ? - 1 : 1;
}

/**
 * Compare two event dates with time precision
 *
 * @param object $a event object.
 * @param object $b event object.
 *
 * @return integer (ternary value)
 */
function mc_timediff_cmp( $a, $b ) {
	$a          = $a . current_time( ' H:i:s' );
	$b          = $b . current_time( ' H:i:s' );
	$event_dt_a = strtotime( $a );
	$event_dt_b = strtotime( $b );
	$diff_a     = mc_date_diff_precise( $event_dt_a );
	$diff_b     = mc_date_diff_precise( $event_dt_b );

	if ( $diff_a === $diff_b ) {
		return 0;
	}

	return ( $diff_a < $diff_b ) ? - 1 : 1;
}

/**
 * Compare two dates for diff with high precision
 *
 * @param int        $start timestamp.
 * @param int|string $end timestamp or 'now'.
 *
 * @return int absolute value of time diff
 */
function mc_date_diff_precise( $start, $end = 'NOW' ) {
	if ( 'NOW' === $end ) {
		$end = strtotime( 'NOW' );
	}
	$sdate = $start;
	$edate = $end;

	$time = $edate - $sdate;

	return abs( $time );
}

/**
 * Get the week of the month a given date falls on.
 *
 * @param integer $date_of_event  current month's date.
 *
 * @return integer $week_of_event The week of the month this date falls in;
 */
function mc_week_of_month( $date_of_event ) {
	$week_of_event = 0;
	switch ( $date_of_event ) {
		case ( $date_of_event >= 1 && $date_of_event < 8 ):
			$week_of_event = 0;
			break;
		case ( $date_of_event >= 8 && $date_of_event < 15 ):
			$week_of_event = 1;
			break;
		case ( $date_of_event >= 15 && $date_of_event < 22 ):
			$week_of_event = 2;
			break;
		case ( $date_of_event >= 22 && $date_of_event < 29 ):
			$week_of_event = 3;
			break;
		case ( $date_of_event >= 29 ):
			$week_of_event = 4;
			break;
	}

	return $week_of_event;
}

/**
 * Validate that a string is a valid date.
 *
 * @param string $date date string.
 *
 * @return boolean|string date string if verified date, false if not.
 */
function mc_checkdate( $date ) {
	$time = strtotime( $date );
	$m    = mc_date( 'n', $time );
	$d    = mc_date( 'j', $time );
	$y    = mc_date( 'Y', $time );

	$check = checkdate( $m, $d, $y );
	if ( $check ) {
		return mc_date( 'Y-m-d', $time, false );
	}

	return false;
}

/**
 * Determine where a date lies in a month in terms of week/day.
 *
 * @param string $ts Timestamp.
 *
 * @return array
 */
function mc_recur_date( $ts ) {
	$ts      = is_numeric( $ts ) ? $ts : strtotime( $ts );
	$weekday = mc_date( 'l', $ts, false );
	$month   = mc_date( 'M', $ts, false );
	$ord     = 1;

	while ( mc_date( 'M', ( $ts = strtotime( '-1 week', $ts ) ), false ) === $month ) { // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
		$ord++;
	}

	return array(
		'num' => $ord,
		'day' => $weekday,
	);
}

/**
 * Get the first day value of the current week.
 *
 * @param int|boolean $timestamp timestamp + offset or false if now.
 *
 * @return array day and month
 */
function mc_first_day_of_week( $timestamp = false ) {
	$start_of_week = ( get_option( 'start_of_week' ) === '1' || get_option( 'start_of_week' ) === '0' ) ? absint( get_option( 'start_of_week' ) ) : 0;
	if ( $timestamp ) {
		$today = mc_date( 'w', $timestamp, false );
		$now   = mc_date( 'Y-m-d', $timestamp, false );
	} else {
		$today = current_time( 'w' );
		$now   = current_time( 'Y-m-d' );
	}
	$month = 0;
	$sub   = 0; // don't change month.
	switch ( $today ) {
		case 1:
			$sub = ( 1 === $start_of_week ) ? 0 : 1;
			break; // mon.
		case 2:
			$sub = ( 1 === $start_of_week ) ? 1 : 2;
			break; // tues.
		case 3:
			$sub = ( 1 === $start_of_week ) ? 2 : 3;
			break; // wed.
		case 4:
			$sub = ( 1 === $start_of_week ) ? 3 : 4;
			break; // thu.
		case 5:
			$sub = ( 1 === $start_of_week ) ? 4 : 5;
			break; // fri.
		case 6:
			$sub = ( 1 === $start_of_week ) ? 5 : 6;
			break; // sat.
		case 0:
			$sub = ( 1 === $start_of_week ) ? 6 : 0;
			break; // sun.
	}
	$day = mc_date( 'j', strtotime( $now . ' -' . $sub . ' day' ), false );
	if ( 0 !== $sub ) {
		if ( mc_date( 'n', strtotime( $now . ' -' . $sub . ' day' ), false ) !== mc_date( 'n', strtotime( $now ), false ) ) {
			$month = - 1;
		} else {
			$month = 0;
		}
	}

	return array( $day, $month );
}

/**
 * Generate an ordinal string in English for numeric values
 *
 * @param int $number Any integer value.
 *
 * @return string number plus ordinal value
 */
function mc_ordinal( $number ) {
	// when fed a number, adds the English ordinal suffix. Works for any number.
	if ( $number % 100 > 10 && $number % 100 < 14 ) {
		$suffix = 'th';
	} else {
		switch ( $number % 10 ) {
			case 0:
				$suffix = 'th';
				break;
			case 1:
				$suffix = 'st';
				break;
			case 2:
				$suffix = 'nd';
				break;
			case 3:
				$suffix = 'rd';
				break;
			default:
				$suffix = 'th';
				break;
		}
	}

	return apply_filters( 'mc_ordinal', "${number}$suffix", $number, $suffix );
}

/**
 * Handles all cases for exiting processing early: private events, drafts, etc.
 *
 * @param object $event Event object.
 * @param string $process_date Current date being articulated.
 *
 * @return boolean true if early exit is qualified.
 */
function mc_exit_early( $event, $process_date ) {
	// if event is not approved, return without processing.
	if ( 1 !== (int) $event->event_approved && ! mc_is_preview() ) {
		return true;
	}

	$hide_days = apply_filters( 'mc_hide_additional_days', false, $event );
	$today     = mc_date( 'Y-m-d', strtotime( $event->occur_begin ) );
	$current   = mc_date( 'Y-m-d', strtotime( $process_date ) );
	$end       = mc_date( 'Y-m-d', strtotime( $event->occur_end ) );
	// if event ends at midnight today (e.g., very first thing of the day), exit without re-drawing.
	// or if event started yesterday & has event_hide_end checked.
	$ends_at_midnight = ( '00:00:00' === $event->event_endtime && $end === $process_date && $current !== $today ) ? true : false;

	// hides events if hiding end time & not first day.
	$hide_day_two = ( $hide_days && ( $today !== $current ) ) ? true : false;

	if ( $ends_at_midnight || $hide_day_two ) {
		return true;
	}

	if ( mc_private_event( $event ) ) {
		return true;
	}

	return false;
}

/**
 * Check whether an object with a category_private property is private &or hidden.
 *
 * @param object $event [can be a category object].
 * @param bool   $type true to check whether an object is hidden; false to check the object configuration.
 *
 * @return boolean
 */
function mc_private_event( $event, $type = true ) {
	// If this is an invalid event, consider it private.
	if ( ! is_object( $event ) || ! property_exists( $event, 'category_private' ) ) {
		return true;
	}
	if ( $type ) {
		// Checking whether this should currently be hidden.
		$status = ( 1 === absint( $event->category_private ) && ! is_user_logged_in() ) ? true : false;
	} else {
		// Checking whether this is supposed to be private.
		$status = ( 1 === absint( $event->category_private ) ) ? true : false;
	}

	/**
	 * Filter the privacy status of an event or category.
	 *
	 * @hook mc_private_event
	 *
	 * @param {bool}   $status true if an event is private, false if it is public.
	 * @param {object} $event A category or event object to test.
	 *
	 * @return {bool}
	 */
	$status = apply_filters( 'mc_private_event', $status, $event );

	return $status;
}

/**
 * Parse a string and replace internationalized months with English so strtotime() will parse correctly
 *
 * @param string $string Date information.
 *
 * @return int de-internationalized change
 */
function mc_strtotime( $string ) {
	$months  = array(
		date_i18n( 'F', strtotime( 'January 1' ) ),
		date_i18n( 'F', strtotime( 'February 1' ) ),
		date_i18n( 'F', strtotime( 'March 1' ) ),
		date_i18n( 'F', strtotime( 'April 1' ) ),
		date_i18n( 'F', strtotime( 'May 1' ) ),
		date_i18n( 'F', strtotime( 'June 1' ) ),
		date_i18n( 'F', strtotime( 'July 1' ) ),
		date_i18n( 'F', strtotime( 'August 1' ) ),
		date_i18n( 'F', strtotime( 'September 1' ) ),
		date_i18n( 'F', strtotime( 'October 1' ) ),
		date_i18n( 'F', strtotime( 'November 1' ) ),
		date_i18n( 'F', strtotime( 'December 1' ) ),
		date_i18n( 'M', strtotime( 'January 1' ) ),
		date_i18n( 'M', strtotime( 'February 1' ) ),
		date_i18n( 'M', strtotime( 'March 1' ) ),
		date_i18n( 'M', strtotime( 'April 1' ) ),
		date_i18n( 'M', strtotime( 'May 1' ) ),
		date_i18n( 'M', strtotime( 'June 1' ) ),
		date_i18n( 'M', strtotime( 'July 1' ) ),
		date_i18n( 'M', strtotime( 'August 1' ) ),
		date_i18n( 'M', strtotime( 'September 1' ) ),
		date_i18n( 'M', strtotime( 'October 1' ) ),
		date_i18n( 'M', strtotime( 'November 1' ) ),
		date_i18n( 'M', strtotime( 'December 1' ) ),
	);
	$english = array(
		'January',
		'February',
		'March',
		'April',
		'May',
		'June',
		'July',
		'August',
		'September',
		'October',
		'November',
		'December',
		'Jan',
		'Feb',
		'Mar',
		'Apr',
		'May',
		'Jun',
		'Jul',
		'Aug',
		'Sep',
		'Oct',
		'Nov',
		'Dec',
	);

	return strtotime( str_replace( $months, $english, $string ) );
}

/**
 * Wrapper for date(). Used for date comparisons and non-translatable dates.
 *
 * @param string    $format Format to use.
 * @param int|false $timestamp Timestamp or false if now.
 * @param bool      $offset false to not add offset; if already a true timestamp.
 *
 * @return string|int Formatted date or timestamp if no format provided.
 */
function mc_date( $format, $timestamp = false, $offset = true ) {
	if ( ! $timestamp ) {
		// Timestamp is UTC.
		$timestamp = time();
	}
	if ( $offset ) {
		$offset = intval( get_option( 'gmt_offset', 0 ) ) * 60 * 60;
	} else {
		$offset = 0;
	}
	$timestamp = $timestamp + $offset;

	return ( '' === $format ) ? $timestamp : gmdate( $format, $timestamp );
}

/**
 * Get the days of the week for calendar layout.
 *
 * @param array $params Calendar parameters.
 * @param int   $start_of_week First day of this week.
 *
 * @return array
 */
function mc_get_week_days( $params, $start_of_week ) {
	$name_days = mc_name_days( $params['format'] );
	$abbrevs   = array( 'sun', 'mon', 'tues', 'wed', 'thur', 'fri', 'sat' );
	if ( 1 === (int) $start_of_week ) {
		$first       = array_shift( $name_days );
		$afirst      = array_shift( $abbrevs );
		$name_days[] = $first;
		$abbrevs[]   = $afirst;
	}
	$return = array(
		'name_days' => $name_days,
		'abbrevs'   => $abbrevs,
	);

	/**
	 * Filter the days of the week as used for column headings in grid format.
	 *
	 * @hook mc_get_week_days
	 * @since 3.4.0
	 *
	 * @param {array} $return Array of full names and abbreviations.
	 * @param {array} $params Array of parameters for this calendar view.
	 * @param {int}   $start_of_week Which day is the start of the week.
	 *
	 * @return {array}
	 */
	return apply_filters( 'mc_get_week_days', $return, $params, $start_of_week );
}

/**
 * Generate abbreviations & code used for HTML output of calendar headings.
 *
 * @param string $format 'mini', 'list', 'grid'.
 *
 * @return array HTML for each day in an array.
 */
function mc_name_days( $format ) {
	$name_days = array(
		'<abbr title="' . date_i18n( 'l', strtotime( 'Sunday' ) ) . '" aria-hidden="true">' . date_i18n( 'D', strtotime( 'Sunday' ) ) . '</abbr><span class="screen-reader-text">' . date_i18n( 'l', strtotime( 'Sunday' ) ) . '</span>',
		'<abbr title="' . date_i18n( 'l', strtotime( 'Monday' ) ) . '" aria-hidden="true">' . date_i18n( 'D', strtotime( 'Monday' ) ) . '</abbr><span class="screen-reader-text">' . date_i18n( 'l', strtotime( 'Monday' ) ) . '</span>',
		'<abbr title="' . date_i18n( 'l', strtotime( 'Tuesday' ) ) . '" aria-hidden="true">' . date_i18n( 'D', strtotime( 'Tuesday' ) ) . '</abbr><span class="screen-reader-text">' . date_i18n( 'l', strtotime( 'Tuesday' ) ) . '</span>',
		'<abbr title="' . date_i18n( 'l', strtotime( 'Wednesday' ) ) . '" aria-hidden="true">' . date_i18n( 'D', strtotime( 'Wednesday' ) ) . '</abbr><span class="screen-reader-text">' . date_i18n( 'l', strtotime( 'Wednesday' ) ) . '</span>',
		'<abbr title="' . date_i18n( 'l', strtotime( 'Thursday' ) ) . '" aria-hidden="true">' . date_i18n( 'D', strtotime( 'Thursday' ) ) . '</abbr><span class="screen-reader-text">' . date_i18n( 'l', strtotime( 'Thursday' ) ) . '</span>',
		'<abbr title="' . date_i18n( 'l', strtotime( 'Friday' ) ) . '" aria-hidden="true">' . date_i18n( 'D', strtotime( 'Friday' ) ) . '</abbr><span class="screen-reader-text">' . date_i18n( 'l', strtotime( 'Friday' ) ) . '</span>',
		'<abbr title="' . date_i18n( 'l', strtotime( 'Saturday' ) ) . '" aria-hidden="true">' . date_i18n( 'D', strtotime( 'Saturday' ) ) . '</abbr><span class="screen-reader-text">' . date_i18n( 'l', strtotime( 'Saturday' ) ) . '</span>',
	);
	if ( 'mini' === $format ) {
		// PHP doesn't have a single letter abbreviation, so this has to be a translatable.
		$name_days = array(
			'<span aria-hidden="true">' . __( '<abbr title="Sunday">S</abbr>', 'my-calendar' ) . '</span><span class="screen-reader-text">' . date_i18n( 'l', strtotime( 'Sunday' ) ) . '</span>', // phpcs:ignore WordPress.WP.I18n.NoHtmlWrappedStrings
			'<span aria-hidden="true">' . __( '<abbr title="Monday">M</abbr>', 'my-calendar' ) . '</span><span class="screen-reader-text">' . date_i18n( 'l', strtotime( 'Monday' ) ) . '</span>', // phpcs:ignore WordPress.WP.I18n.NoHtmlWrappedStrings
			'<span aria-hidden="true">' . __( '<abbr title="Tuesday">T</abbr>', 'my-calendar' ) . '</span><span class="screen-reader-text">' . date_i18n( 'l', strtotime( 'Tuesday' ) ) . '</span>', // phpcs:ignore WordPress.WP.I18n.NoHtmlWrappedStrings
			'<span aria-hidden="true">' . __( '<abbr title="Wednesday">W</abbr>', 'my-calendar' ) . '</span><span class="screen-reader-text">' . date_i18n( 'l', strtotime( 'Wednesday' ) ) . '</span>', // phpcs:ignore WordPress.WP.I18n.NoHtmlWrappedStrings
			'<span aria-hidden="true">' . __( '<abbr title="Thursday">T</abbr>', 'my-calendar' ) . '</span><span class="screen-reader-text">' . date_i18n( 'l', strtotime( 'Thursday' ) ) . '</span>', // phpcs:ignore WordPress.WP.I18n.NoHtmlWrappedStrings
			'<span aria-hidden="true">' . __( '<abbr title="Friday">F</abbr>', 'my-calendar' ) . '</span><span class="screen-reader-text">' . date_i18n( 'l', strtotime( 'Friday' ) ) . '</span>', // phpcs:ignore WordPress.WP.I18n.NoHtmlWrappedStrings
			'<span aria-hidden="true">' . __( '<abbr title="Saturday">S</abbr>', 'my-calendar' ) . '</span><span class="screen-reader-text">' . date_i18n( 'l', strtotime( 'Saturday' ) ) . '</span>', // phpcs:ignore WordPress.WP.I18n.NoHtmlWrappedStrings
		);
	}

	return $name_days;
}

/**
 * Calculate dates that should be used to set start and end dates for current view.
 *
 * @param string $timestamp Time stamp for first date of current period.
 * @param string $period base type of date span displayed.
 * @param int    $months Number of months to add to display.
 *
 * @return array from and to dates
 */
function mc_date_array( $timestamp, $period, $months = 0 ) {
	switch ( $period ) {
		case 'month':
		case 'month+1':
			if ( 'month+1' === $period ) {
				$timestamp = strtotime( '+1 month', $timestamp );
			}
			$start_of_week = get_option( 'start_of_week' );
			$first         = mc_date( 'N', $timestamp, false ); // ISO-8601.
			$sub           = mc_date( 'w', $timestamp, false ); // numeric (how WordPress option is stored).
			$n             = ( 1 === (int) $start_of_week ) ? $first - 1 : $first;

			if ( $sub === $start_of_week ) {
				$from = mc_date( 'Y-m-d', $timestamp, false );
			} else {
				$start = strtotime( "-$n days", $timestamp );
				$from  = mc_date( 'Y-m-d', $start, false );
			}
			$endtime = mktime( 0, 0, 0, mc_date( 'm', $timestamp, false ), mc_date( 't', $timestamp, false ), mc_date( 'Y', $timestamp, false ) );
			$endtime = strtotime( "+$months months", $endtime );
			$last    = (int) mc_date( 'N', $endtime, false );
			$n       = ( '1' === get_option( 'start_of_week' ) ) ? 7 - $last : 6 - $last;
			if ( -1 === $n && '7' === mc_date( 'N', $endtime, false ) ) {
				$n = 6;
			}
			$to = mc_date( 'Y-m-d', strtotime( "+$n days", $endtime ), false );

			$return = array(
				'from' => $from,
				'to'   => $to,
			);
			break;
		case 'week':
			$day_of_week   = (int) mc_date( 'N', $timestamp, false );
			$start_of_week = ( get_option( 'start_of_week' ) === '1' ) ? 1 : 7; // convert start of week to ISO 8601 (Monday/Sunday).
			if ( $day_of_week !== $start_of_week ) {
				if ( $start_of_week > $day_of_week ) {
					$diff = 7 - ( ( $start_of_week - $day_of_week ) );
				} else {
					$diff = ( $day_of_week - $start_of_week );
				}
				$timestamp = ( 7 !== $diff ) ? strtotime( "-$diff days", $timestamp ) : $timestamp;
			}
			$from = mc_date( 'Y-m-d', $timestamp, false );
			$to   = mc_date( 'Y-m-d', strtotime( '+6 days', $timestamp ), false );

			$return = array(
				'from' => $from,
				'to'   => $to,
			);
			break;
		default:
			$return = false;
	}

	return $return;
}

/**
 * Get from and to values for current view
 *
 * @param int   $show_months List view parameter.
 * @param array $params Calendar view parameters.
 * @param array $date Current date viewed.
 *
 * @return array from & to dates
 */
function mc_get_from_to( $show_months, $params, $date ) {
	$format = $params['format'];
	$time   = $params['time'];
	// The value is total months to show; need additional months to show.
	$num     = $show_months - 1;
	$c_month = $date['month'];
	$c_year  = $date['year'];
	// The first day of the current month.
	$month_first = mktime( 0, 0, 0, $c_month, 1, $c_year );
	// Grid calendar can't show multiple months.
	if ( 'list' === $format && 'week' !== $time ) {
		if ( $num > 0 && 'day' !== $time && 'week' !== $time ) {
			if ( 'month+1' === $time ) {
				$from = mc_date( 'Y-m-d', strtotime( '+1 month', $month_first ), false );
				$next = strtotime( "+$num months", strtotime( '+1 month', $month_first ) );
			} else {
				$from = mc_date( 'Y-m-d', $month_first, false );
				$next = strtotime( "+$num months", $month_first );
			}
			$last = mc_date( 't', $next, false );
			$to   = mc_date( 'Y-m', $next, false ) . '-' . $last;
		} else {
			$from = mc_date( 'Y-m-d', $month_first, false );
			$to   = mc_date( 'Y-m-d', mktime( 0, 0, 0, $c_month, mc_date( 't', $month_first, false ), $c_year ), false );
		}
		$dates = array(
			'from' => $from,
			'to'   => $to,
		);
	} else {
		// Get a view based on current date.
		$dates = mc_date_array( $date['current_date'], $time, $num );
	}

	return $dates;
}

/**
 * Test whether an event is in the past, currently happening, or in the future.
 *
 * @param object $event Event object.
 *
 * @return int
 */
function mc_date_relation( $event ) {
	$ts            = $event->ts_occur_begin;
	$end           = $event->ts_occur_end;
	$date_relation = 2;
	$now           = time();
	if ( $ts < $now && $end > $now ) {
		/**
		 * Execute action while an event is happening.
		 *
		 * @hook mc_event_happening
		 *
		 * @param {object} $object Event object.
		 */
		do_action( 'mc_event_happening', $event );
		$date_relation = 1;
	} elseif ( $now < $ts ) {
		/**
		 * Execute action before an event will occur.
		 *
		 * @hook mc_event_future
		 *
		 * @param {object} $object Event object.
		 */
		do_action( 'mc_event_future', $event );
		$date_relation = 2;
	} elseif ( $now > $ts ) {
		/**
		 * Execute action after an event has occurred.
		 *
		 * @hook mc_event_over
		 *
		 * @param {object} $object Event object.
		 */
		do_action( 'mc_event_over', $event );
		$date_relation = 0;
	}

	return $date_relation;
}

/**
 * Get the date format for My Calendar primary views.
 *
 * @return string
 */
function mc_date_format() {
	$date_format = ( '' === mc_get_option( 'date_format' ) ) ? get_option( 'date_format' ) : mc_get_option( 'date_format' );

	return $date_format;
}

/**
 * Produce the human-readable string for recurrence.
 *
 * @param object $event Event object.
 * @param array  $args Recurrence settings.
 *
 * @return string Type of recurrence
 */
function mc_recur_string( $event, $args = array() ) {
	if ( ! $event ) {
		$recur = $args['recur'];
		$every = (int) $args['every'];
		$until = $args['until'];
	} else {
		$recurs = str_split( $event->event_recur, 1 );
		$recur  = $recurs[0];
		$every  = ( isset( $recurs[1] ) ) ? str_replace( $recurs[0], '', $event->event_recur ) : 1;
		$until  = $event->event_repeats;
	}
	$string = '';
	// Interpret the DB values into something human readable.
	switch ( $recur ) {
		case 'D':
			// Translators: number of days between repetitions.
			$string = ( 1 === (int) $every ) ? __( 'Daily', 'my-calendar' ) : sprintf( __( 'Every %s day', 'my-calendar' ), mc_ordinal( $every ) );
			break;
		case 'E':
			// Translators: number of days between repetitions.
			$string = ( 1 === (int) $every ) ? __( 'Weekdays', 'my-calendar' ) : sprintf( __( 'Every %s weekday', 'my-calendar' ), mc_ordinal( $every ) );
			break;
		case 'W':
			// Translators: number of weeks between repetitions.
			$string = ( 1 === (int) $every ) ? __( 'Weekly', 'my-calendar' ) : sprintf( __( 'Every %d weeks', 'my-calendar' ), $every );
			break;
		case 'B':
			$string = __( 'Bi-Weekly', 'my-calendar' );
			break;
		case 'M':
			// Translators: number of months between repetitions.
			$string = ( 1 === (int) $every ) ? __( 'Monthly (by date)', 'my-calendar' ) : sprintf( __( 'Every %d months (by date)', 'my-calendar' ), $every );
			break;
		case 'U':
			$string = __( 'Monthly (by day)', 'my-calendar' );
			break;
		case 'Y':
			// Translators: number of years between repetitions.
			$string = ( 1 === (int) $every ) ? __( 'Yearly', 'my-calendar' ) : sprintf( __( 'Every %d years', 'my-calendar' ), $every );
			break;
	}
	$eternity = _mc_increment_values( $recur );
	if ( $until && 'S' !== $recur ) {
		if ( is_numeric( $until ) ) {
			// Translators: number of repeats.
			$string .= ' ' . sprintf( __( '- %d Times', 'my-calendar' ), $until );
		} else {
			// Translators: date repeating until.
			$string .= ' ' . sprintf( __( ' until %s', 'my-calendar' ), $until );
		}
	} elseif ( $eternity ) {
		// Translators: number of repeats.
		$string .= ' ' . sprintf( __( '- %d Times', 'my-calendar' ), $eternity );
	}

	return $string;
}