Automattic\WooCommerce\Admin\API\Reports

TimeInterval::get_timeframe_dates()public staticWC 1.0

Get dates from a timeframe string.

{} Это метод класса: TimeInterval{}

Хуков нет.

Возвращает

Массив.

Использование

$result = TimeInterval::get_timeframe_dates( $timeframe, $current_date );
$timeframe(int) (обязательный)
Timeframe to use. One of: last_week|last_month|last_quarter|last_6_months|last_year.
$current_date(DateTime|null)
DateTime of current date to compare.
По умолчанию: null

Код TimeInterval::get_timeframe_dates() WC 6.9.4

public static function get_timeframe_dates( $timeframe, $current_date = null ) {
	if ( ! $current_date ) {
		$current_date = new \DateTime();
	}
	$current_year  = $current_date->format( 'Y' );
	$current_month = $current_date->format( 'm' );

	if ( 'last_week' === $timeframe ) {
		return array(
			'start' => $current_date->modify( 'last week monday' )->format( 'Y-m-d 00:00:00' ),
			'end'   => $current_date->modify( 'this sunday' )->format( 'Y-m-d 23:59:59' ),
		);
	}

	if ( 'last_month' === $timeframe ) {
		return array(
			'start' => $current_date->modify( 'first day of previous month' )->format( 'Y-m-d 00:00:00' ),
			'end'   => $current_date->modify( 'last day of this month' )->format( 'Y-m-d 23:59:59' ),
		);
	}

	if ( 'last_quarter' === $timeframe ) {
		switch ( $current_month ) {
			case $current_month >= 1 && $current_month <= 3:
				return array(
					'start' => ( $current_year - 1 ) . '-10-01 00:00:00',
					'end'   => ( $current_year - 1 ) . '-12-31 23:59:59',
				);
			case $current_month >= 4 && $current_month <= 6:
				return array(
					'start' => $current_year . '-01-01 00:00:00',
					'end'   => $current_year . '-03-31 23:59:59',
				);
			case $current_month >= 7 && $current_month <= 9:
				return array(
					'start' => $current_year . '-04-01 00:00:00',
					'end'   => $current_year . '-06-30 23:59:59',
				);
			case $current_month >= 10 && $current_month <= 12:
				return array(
					'start' => $current_year . '-07-01 00:00:00',
					'end'   => $current_year . '-09-31 23:59:59',
				);
		}
	}

	if ( 'last_6_months' === $timeframe ) {
		if ( $current_month >= 1 && $current_month <= 6 ) {
			return array(
				'start' => ( $current_year - 1 ) . '-07-01 00:00:00',
				'end'   => ( $current_year - 1 ) . '-12-31 23:59:59',
			);
		}
		return array(
			'start' => $current_year . '-01-01 00:00:00',
			'end'   => $current_year . '-06-30 23:59:59',
		);
	}

	if ( 'last_year' === $timeframe ) {
		return array(
			'start' => ( $current_year - 1 ) . '-01-01 00:00:00',
			'end'   => ( $current_year - 1 ) . '-12-31 23:59:59',
		);
	}

	return false;
}