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_string
stringrequired- The date to be converted, in the timezone of the site.
$format
stringoptional- The format string for the returned date.
Default:
'Y-m-d H:i:s'
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
Version | Description |
---|---|
1.2.0 | Introduced. |
The inverse of this is get_date_from_gmt
https://developer.wordpress.org/reference/functions/get_date_from_gmt/