WordPress at a glance

get_gmt_from_date() WP 1.0

Returns a date in the GMT equivalent.

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.

Works based on: get_option()
✈ 1 time = 0.000157s = fast | 50000 times = 2.98s = fast

No Hooks.

Return

String. GMT version of the date provided.

Usage

get_gmt_from_date( $string, $format );
$string(string) (required)
The date to be converted.
$format(string)
The format string for the returned date (default is Y-m-d H:i:s)
Default: 'Y-m-d H:i:s'

Code of get_gmt_from_date: wp-includes/formatting.php VER 4.9.8

<?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;
}

Related Functions

From tag: date time

No comments
    Hello, !     Log In . Register