rest_parse_date() WordPress Function

The rest_parse_date() function is a utility function that handles date parsing for the WordPress REST API. It accepts a date string in a variety of formats and returns a DateTime object. This function is used internally by the WordPress REST API to parse dates passed in request parameters.

rest_parse_date( string $date, bool $force_utc = false ) #

Parses an RFC3339 time into a Unix timestamp.


Parameters

$date

(string)(Required)RFC3339 timestamp.

$force_utc

(bool)(Optional) Whether to force UTC timezone instead of using the timestamp's timezone.

Default value: false


Top ↑

Return

(int) Unix timestamp.


Top ↑

Source

File: wp-includes/rest-api.php

function rest_parse_date( $date, $force_utc = false ) {
	if ( $force_utc ) {
		$date = preg_replace( '/[+-]\d+:?\d+$/', '+00:00', $date );
	}

	$regex = '#^\d{4}-\d{2}-\d{2}[Tt ]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}(?::\d{2})?)?$#';

	if ( ! preg_match( $regex, $date, $matches ) ) {
		return false;
	}

	return strtotime( $date );
}


Top ↑

Changelog

Changelog
VersionDescription
4.4.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More