Week_View::get_previous_event_date()

Get the date of the event immediately previous to the current view date.


Parameters #

$current_date

(TribeEventsProViewsV2ViewsDateTime) (Required) A DateTime object signifying the current date for the view.


Top ↑

Return #

(TribeEventsProViewsV2ViewsDateTime|false) Either the previous event chronologically, the previous month, or false if no next event found.


Top ↑

Source #

File: src/Tribe/Views/V2/Views/Week_View.php

	public function get_previous_event_date() {
		$args = $this->filter_repository_args( parent::setup_repository_args( $this->context ) );
		$date         = $this->context->get( 'event_date', 'today' );
		$current_date = Dates::build_date_object( $date );

		// Use cache to reduce the performance impact.
		$cache_key = __METHOD__ . '_' . substr( md5( wp_json_encode( [ $current_date, $args ] ) ), 10 );

		if ( isset( $this->cached_event_dates[ $cache_key ] ) ) {
			return $this->cached_event_dates[ $cache_key ];
		}

		list( $week_start ) = $this->calculate_grid_start_end( $current_date );

		// Find the first event that starts before the start of this month.
		$prev_event = tribe_events()
			->by_args( $args )
			->where( 'ends_before', tribe_beginning_of_day( $week_start->format( 'Y-m-d' ) ) )
			->order( 'DESC' )
			->first();

		if ( ! $prev_event instanceof \WP_Post ) {
			return false;
		}

		// Show the closest week on which that event appears (but not the current week).
		$prev_date  = min(
			Dates::build_date_object( $prev_event->dates->start ),
			$current_date->modify( '-1 week' )
		);

		$prev_date = $prev_date->format( 'W' );

		$this->cached_event_dates[ $cache_key ] = $prev_date;

		return $prev_date;
	}

Top ↑

Changelog #

Changelog
Version Description
5.14.2 Introduced.