iso8601_to_datetime() WordPress Function

The iso8601_to_datetime() function converts an ISO 8601 date string to a date/time object.

iso8601_to_datetime( string $date_string, string $timezone = 'user' ) #

Given an ISO 8601 (Ymd\TH:i:sO) date, returns a MySQL DateTime (Y-m-d H:i:s) format used by post_date[_gmt].


Parameters

$date_string

(string)(Required)Date and time in ISO 8601 format https://en.wikipedia.org/wiki/ISO_8601.

$timezone

(string)(Optional) If set to 'gmt' returns the result in UTC.

Default value: 'user'


Top ↑

Return

(string|false) The date and time in MySQL DateTime format

  • Y-m-d H:i:s, or false on failure.


Top ↑

Source

File: wp-includes/formatting.php

function iso8601_to_datetime( $date_string, $timezone = 'user' ) {
	$timezone    = strtolower( $timezone );
	$wp_timezone = wp_timezone();
	$datetime    = date_create( $date_string, $wp_timezone ); // Timezone is ignored if input has one.

	if ( false === $datetime ) {
		return false;
	}

	if ( 'gmt' === $timezone ) {
		return $datetime->setTimezone( new DateTimeZone( 'UTC' ) )->format( 'Y-m-d H:i:s' );
	}

	if ( 'user' === $timezone ) {
		return $datetime->setTimezone( $wp_timezone )->format( 'Y-m-d H:i:s' );
	}

	return false;
}


Top ↑

Changelog

Changelog
VersionDescription
1.5.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