get_date_from_gmt()WP 1.2.0

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

Requires a date in the Y-m-d H:i:s format. Default return format of 'Y-m-d H:i:s' can be overridden using the $format parameter.

1 time — 0.000149 sec (fast) | 50000 times — 3.75 sec (fast)

No Hooks.

Return

String. Formatted version of the date, in the site's timezone.

Usage

get_date_from_gmt( $string, $format );
$string(string) (required)
The date to be converted, in UTC or GMT timezone.
$format(string)
The format string for the returned date.
Default: 'Y-m-d H:i:s'

Examples

0

#1 Get the local time of the site from the GMT time

This example is valid if time zone is selected on page wp-admin/options-general.php, for example UTC+4.

echo get_date_from_gmt( '2015-09-25 13:56:43' );
// Posted: 2015-09-25 17:56:43
0

#2 Demo Usage

This function does not accept a Unix Timestamp because date_create() doesn’t.

If you need to provide a Unix Timestamp, you will need to convert it to a different format first like the following example:

<?php

$utc_timestamp = 1623096269;

// This is a format that date_create() will accept
$utc_timestamp_converted = date( 'Y-m-d H:i:s', $utc_timestamp );

$output_format = 'Y-m-d H:i:s';

// Now we can use our timestamp
$local_timestamp = get_date_from_gmt( $utc_timestamp_converted, $output_format );

Changelog

Since 1.2.0 Introduced.

Code of get_date_from_gmt() WP 5.9.3

function get_date_from_gmt( $string, $format = 'Y-m-d H:i:s' ) {
	$datetime = date_create( $string, new DateTimeZone( 'UTC' ) );

	if ( false === $datetime ) {
		return gmdate( $format, 0 );
	}

	return $datetime->setTimezone( wp_timezone() )->format( $format );
}