get_gmt_from_date( string $date_string, string $format = 'Y-m-d H:i:s' )
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
-
(Required) The date to be converted, in the timezone of the site.
- $format
-
(Optional) The format string for the returned date. Default 'Y-m-d H:i:s'.
Default value: 'Y-m-d H:i:s'
Return
(string) Formatted version of the date, in UTC.
Source
File: wp-includes/formatting.php
function get_gmt_from_date( $string, $format = 'Y-m-d H:i:s' ) {
$tz = get_option( 'timezone_string' );
if ( $tz ) {
$datetime = date_create( $string, new DateTimeZone( $tz ) );
if ( ! $datetime ) {
return gmdate( $format, 0 );
}
$datetime->setTimezone( new DateTimeZone( 'UTC' ) );
$string_gmt = $datetime->format( $format );
} else {
if ( ! preg_match( '#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches ) ) {
$datetime = strtotime( $string );
if ( false === $datetime ) {
return gmdate( $format, 0 );
}
return gmdate( $format, $datetime );
}
$string_time = gmmktime( $matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1] );
$string_gmt = gmdate( $format, $string_time - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
}
return $string_gmt;
}
Changelog
Version | Description |
---|---|
1.2.0 | Introduced. |