get_gmt_from_date()WP 1.2.0

Converts the specified date to the site's timezone, to GMT/UTC zone. Accepts date in the format Y-m-d H:i:s.

It is implied that a NON GMT date will be passed, but a local date.

The format of the returned date can be overridden in the second parameter. However, the passed date must always be in the format Y-m-d H:i:s.

Translates the date based on the option timezone_string (example value: Asia/Tashkent). If such an option is not available, then the setting gmt_offset is used (example value: 5, 6, -3, -4...).

To, on the contrary, get the local date from GMT, use get_date_from_gmt()

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

No Hooks.

Returns

String. Date in the specified format, in UTC/GMT zone.

Usage

get_gmt_from_date( $date_string, $format );
$string(string) (required)
Date to be converted. Accepts date in the format Y-m-d H:i:s.
$format(string)
Format of the returned string. Possible formats
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.

get_gmt_from_date() code WP 6.9

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 );
}