Tribe__Date_Utils::get_week_start_end( string|int|DateTime $date, int|null $start_of_week = null )

Returns the DateTime object representing the start of the week for a date.


Parameters

$date

(string|int|DateTime) (Required) The date string, timestamp or object.

$start_of_week

(int|null) (Optional) The number representing the start of week day as handled by WordPress: 0 (for Sunday) through 6 (for Saturday).

Default value: null


Top ↑

Return

(array) An array of objects representing the week start and end days, or false if the supplied date is invalid. The timezone of the returned object is set to the site one. The week start has its time set to 00:00:00, the week end will have its time set 23:59:59.


Top ↑

Source

File: src/Tribe/Date_Utils.php

		public static function get_week_start_end( $date, $start_of_week = null ) {
			$week_start = static::build_date_object( $date );
			$week_start->setTime( 0, 0, 0 );

			// `0` (for Sunday) through `6` (for Saturday); we correct Sunday to stick w/ ISO notation.
			$week_start_day = null !== $start_of_week ? (int) $start_of_week : (int) get_option( 'start_of_week', 0 );
			if ( 0 === $week_start_day ) {
				$week_start_day = 7;
			}
			// `1` (for Monday) through `7` (for Sunday).
			$date_day = (int) $week_start->format( 'N' );

			/*
			 * From the PHP docs, the `W` format stands for:
			 * - ISO-8601 week number of year, weeks starting on Monday
			 * We compensate for weeks starting on Sunday here.
			 */
			$week_offset = array_sum(
				[
					// If the week starts on Sunday move to the next week.
					0 === $week_start_day ? 1 : 0,
					// If the current date is before the start of the week, move back a week.
					$date_day < $week_start_day ? - 1 : 0,
				]
			);
			$week_start->setISODate(
				(int) $week_start->format( 'o' ),
				(int) $week_start->format( 'W' ) + $week_offset,
				$week_start_day
			);

			$week_end = clone $week_start;
			// Add 6 days, then move at the end of the day.
			$week_end->add( new DateInterval( 'P6D' ) );
			$week_end->setTime( 23, 59, 59 );

			return [ $week_start, $week_end ];
		}

Top ↑

Changelog

Changelog
Version Description
4.9.21 Introduced.