Automattic\WooCommerce\Internal\Admin\Reports

HposLegacyOrderReportQueryBuilder::local_datetime_to_gmt │ private │ WC 1.0

Convert a site-local date or datetime string into the equivalent GMT datetime string.

Only plain Y-m-d, Y-m-d H:i and Y-m-d H:i:s values are converted; anything else (including relative formats) returns null so callers can fall back to per-row SQL conversion instead of guessing at the caller's intent.

Method of the class: HposLegacyOrderReportQueryBuilder{}

No Hooks.

Returns

string|null. GMT datetime in Y-m-d H:i:s format, or null when not convertible.

Usage

// private - for code of main (parent) class only
$result = $this->local_datetime_to_gmt( $local_datetime ): ?string;
$local_datetime(string) (required)
Local date or datetime string.

HposLegacyOrderReportQueryBuilder::local_datetime_to_gmt() code WC 11.1.2

private function local_datetime_to_gmt( string $local_datetime ): ?string {
	if ( ! preg_match( '/^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}(:\d{2})?)?$/', $local_datetime ) ) {
		return null;
	}

	try {
		$local = new \DateTimeImmutable( $local_datetime, wp_timezone() );
	} catch ( \Exception $e ) {
		return null;
	}

	return $local->setTimezone( new \DateTimeZone( 'UTC' ) )->format( 'Y-m-d H:i:s' );
}