Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableQuery::local_time_to_gmt_date_queryprivateWC 8.2.0

Returns UTC-based date query arguments for a combination of local time dates and a date shorthand operator.

Method of the class: OrdersTableQuery{}

No Hooks.

Returns

Array. Partial date query arg with relevant dates now UTC-based.

Usage

// private - for code of main (parent) class only
$result = $this->local_time_to_gmt_date_query( $dates_raw, $operator );
$dates_raw(array) (required)
Array of dates (in local time) to use in combination with the operator.
$operator(string) (required)
One of the operators supported by date queries (<, <=, =, ..., >, >smile.

Changelog

Since 8.2.0 Introduced.

OrdersTableQuery::local_time_to_gmt_date_query() code WC 9.9.3

private function local_time_to_gmt_date_query( $dates_raw, $operator ) {
	$result = array();

	// Convert YYYY-MM-DD to UTC timestamp. Per https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query#date only date is relevant (time is ignored).
	foreach ( $dates_raw as &$raw_date ) {
		$raw_date = is_numeric( $raw_date ) ? $raw_date : strtotime( get_gmt_from_date( date( 'Y-m-d', strtotime( $raw_date ) ) ) );
	}

	$date1 = end( $dates_raw );

	switch ( $operator ) {
		case '>':
			$result = array(
				'after'     => $this->date_to_date_query_arg( $date1 + DAY_IN_SECONDS ),
				'inclusive' => true,
			);
			break;
		case '>=':
			$result = array(
				'after'     => $this->date_to_date_query_arg( $date1 ),
				'inclusive' => true,
			);
			break;
		case '=':
			$result = array(
				'relation' => 'AND',
				array(
					'after'     => $this->date_to_date_query_arg( $date1 ),
					'inclusive' => true,
				),
				array(
					'before'    => $this->date_to_date_query_arg( $date1 + DAY_IN_SECONDS ),
					'inclusive' => false,
				),
			);
			break;
		case '<=':
			$result = array(
				'before'    => $this->date_to_date_query_arg( $date1 + DAY_IN_SECONDS ),
				'inclusive' => false,
			);
			break;
		case '<':
			$result = array(
				'before'    => $this->date_to_date_query_arg( $date1 ),
				'inclusive' => false,
			);
			break;
		case '...':
			$result = array(
				'relation' => 'AND',
				$this->local_time_to_gmt_date_query( array( $dates_raw[1] ), '<=' ),
				$this->local_time_to_gmt_date_query( array( $dates_raw[0] ), '>=' ),
			);

			break;
	}

	if ( ! $result ) {
		throw new \Exception( 'Please specify a valid date shorthand operator.' );
	}

	return $result;
}