get_gmt_from_date

Returns a date in the GMT equivalent.

Description

(string) get_gmt_from_date( (string) $string, (string) $format = 'Y-m-d H:i:s' ); 

Requires and returns a date in the Y-m-d H:i:s format. If there is a timezone_string available, the date is assumed to be in that timezone, otherwise it simply subtracts the value of the gmt_offset option. Return format can be overridden using the $format parameter.

Returns (string)

GMT version of the date provided.

Parameters (2)

0. $string (string)
The date to be converted.
1. $format — Optional. (string) => 'Y-m-d H:i:s'
The format string for the returned date (default is Y-m-d H:i:s)

Usage

  1. if ( !function_exists( 'get_gmt_from_date' ) ) { 
  2. require_once ABSPATH . WPINC . '/formatting.php'; 
  3.  
  4. // The date to be converted. 
  5. $string = ''; 
  6.  
  7. // The format string for the returned date (default is Y-m-d H:i:s) 
  8. $format = 'Y-m-d H:i:s'; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_gmt_from_date($string, $format); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/formatting.php  
  1. function get_gmt_from_date( $string, $format = 'Y-m-d H:i:s' ) { 
  2. $tz = get_option( 'timezone_string' ); 
  3. if ( $tz ) { 
  4. $datetime = date_create( $string, new DateTimeZone( $tz ) ); 
  5. if ( ! $datetime ) { 
  6. return gmdate( $format, 0 ); 
  7. $datetime->setTimezone( new DateTimeZone( 'UTC' ) ); 
  8. $string_gmt = $datetime->format( $format ); 
  9. } else { 
  10. 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 ) ) { 
  11. $datetime = strtotime( $string ); 
  12. if ( false === $datetime ) { 
  13. return gmdate( $format, 0 ); 
  14. return gmdate( $format, $datetime ); 
  15. $string_time = gmmktime( $matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1] ); 
  16. $string_gmt = gmdate( $format, $string_time - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ); 
  17. return $string_gmt;