Day_View::get_previous_event_date( TribeEventsViewsV2ViewsDateTime|false $current_date )

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


Parameters #

$current_date

(TribeEventsViewsV2ViewsDateTime|false) (Required) A DateTime object signifying the current date for the view.


Top ↑

Return #

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


Top ↑

Source #

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

	public function get_previous_event_date( $current_date ) {
		$context = $this->context instanceof Tribe__Context ? $this->context : null;
		$args = $this->filter_repository_args( $this->setup_repository_args( $context ) );
		// This value will mess up our query.
		unset( $args['date_overlaps'] );

		// 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 ];
		}

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

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

		// Show the closest date on which that event appears (but not the current date).
		$prev_event_date = Dates::build_date_object( $prev_event->dates->start );
		$prev_date       = min(
			$prev_event_date,
			$current_date->sub( new \DateInterval( 'P1D' ) )
		);

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

		return $prev_date;
	}

Top ↑

Changelog #

Changelog
Version Description
5.16.1 Introduced.