get_date_from_gmt

Converts a GMT date into the correct format for the blog.

Description

(string) get_date_from_gmt( (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 returned date is in that timezone, otherwise it simply adds the value of gmt_offset. Return format can be overridden using the $format parameter

Returns (string)

Formatted date relative to the timezone / GMT offset.

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_date_from_gmt' ) ) { 
  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_date_from_gmt($string, $format); 
  12.  

Defined (1)

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

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