wc_rest_prepare_date_response( string|null|WC_DateTime $date, bool $utc = true )

Parses and formats a date for ISO8601/RFC3339.


Description Description

Required WP 4.4 or later. See https://developer.wordpress.org/reference/functions/mysql_to_rfc3339/


Parameters Parameters

$date

(Required) Date.

$utc

(Optional) Send false to get local/offset time.

Default value: true


Top ↑

Return Return

(string|null) ISO8601/RFC3339 formatted datetime.


Top ↑

Source Source

File: includes/wc-rest-functions.php

function wc_rest_prepare_date_response( $date, $utc = true ) {
	if ( is_numeric( $date ) ) {
		$date = new WC_DateTime( "@$date", new DateTimeZone( 'UTC' ) );
		$date->setTimezone( new DateTimeZone( wc_timezone_string() ) );
	} elseif ( is_string( $date ) ) {
		$date = new WC_DateTime( $date, new DateTimeZone( 'UTC' ) );
		$date->setTimezone( new DateTimeZone( wc_timezone_string() ) );
	}

	if ( ! is_a( $date, 'WC_DateTime' ) ) {
		return null;
	}

	// Get timestamp before changing timezone to UTC.
	return gmdate( 'Y-m-d\TH:i:s', $utc ? $date->getTimestamp() : $date->getOffsetTimestamp() );
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.6.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.