get_gmt_from_date()
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()
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
#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() 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 );
}