get_gmt_from_date()WP 1.2.0

Returns a date in the GMT equivalent.

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

1 time — 0.000157 sec (fast) | 50000 times — 2.98 sec (fast)

No Hooks.

Return

String. Formatted version of the date, in UTC.

Usage

get_gmt_from_date( $string, $format );
$string(string) (required)
The date to be converted, in the timezone of the site.
$format(string)
The format string for the returned date.
Default: 'Y-m-d H:i:s'

Examples

0

#1 Convert local date to GMT

This example is valid if time zone is selected on page wp-admin/options-general.php, for example UTC+4.

echo get_gmt_from_date('2015-09-25 13:56:43');
// Posted: 2015-09-25 17:56:43

Changelog

Since 1.2.0 Introduced.

Code of get_gmt_from_date() WP 5.9.3

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

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

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