Automattic\WooCommerce\Admin\Features\Fulfillments

Fulfillment::normalize_date_to_utcprivateWC 10.8.0

Normalize a date input to a UTC 'Y-m-d H:i:s' string.

Bare MySQL-format strings are interpreted as site-local time (matching the convention of current_time('mysql')). Strings that include an explicit timezone designator (Z, numeric offset, or named zone) are respected as-is.

Method of the class: Fulfillment{}

No Hooks.

Returns

String|null. UTC datetime string, or null for empty/invalid input.

Usage

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

Changelog

Since 10.8.0 Introduced.

Fulfillment::normalize_date_to_utc() code WC 10.9.4

private function normalize_date_to_utc( ?string $date ): ?string {
	$date = null === $date ? null : trim( $date );
	if ( null === $date || '' === $date ) {
		return null;
	}
	try {
		// The second DateTimeZone is used only when the string has no explicit zone.
		$datetime = new \DateTime( $date, wp_timezone() );
		// DateTime silently normalizes invalid calendar dates (e.g. Feb 30 -> Mar 2);
		// reject those so callers don't persist a different date than the user supplied.
		$parse_errors = \DateTime::getLastErrors();
		if ( false !== $parse_errors && ( $parse_errors['warning_count'] > 0 || $parse_errors['error_count'] > 0 ) ) {
			return null;
		}
		$datetime->setTimezone( new \DateTimeZone( 'UTC' ) );
		return $datetime->format( 'Y-m-d H:i:s' );
	} catch ( \Exception $e ) {
		return null;
	}
}