get_gmt_from_date( string $date_string, string $format = ‘Y-m-d H:i:s’ ): string

Given a date in the timezone of the site, returns that date in UTC.

Description

Requires and returns a date in the Y-m-d H:i:s format.
Return format can be overridden using the $format parameter.

Parameters

$date_stringstringrequired
The date to be converted, in the timezone of the site.
$formatstringoptional
The format string for the returned date.

Default:'Y-m-d H:i:s'

Return

string Formatted version of the date, in UTC.

Source

function get_gmt_from_date( $date_string, $format = 'Y-m-d H:i:s' ) {
	$datetime = date_create( $date_string, wp_timezone() );

	if ( false === $datetime ) {
		return gmdate( $format, 0 );
	}

	return $datetime->setTimezone( new DateTimeZone( 'UTC' ) )->format( $format );
}

Changelog

VersionDescription
1.2.0Introduced.

User Contributed Notes

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