Using WordPress ‘get_date_from_gmt()’ PHP function

The get_date_from_gmt() WordPress PHP function converts a given date in UTC or GMT timezone to the site’s timezone.

Usage

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

Parameters

  • $date_string (string) – Required. The date to be converted, in UTC or GMT timezone.
  • $format (string) – Optional. The format string for the returned date. Default: ‘Y-m-d H:i:s’

More information

See WordPress Developer Resources: get_date_from_gmt()

Examples

Convert UTC date to site’s timezone

Convert a date string in UTC format to the site’s timezone with the default format.

$utc_date = '2023-05-08 12:30:00';
$local_date = get_date_from_gmt( $utc_date );

Convert UTC date to site’s timezone with custom format

Convert a date string in UTC format to the site’s timezone with a custom format.

$utc_date = '2023-05-08 12:30:00';
$format = 'm/d/Y H:i';
$local_date = get_date_from_gmt( $utc_date, $format );

Convert a Unix timestamp to site’s timezone

Convert a Unix timestamp to the site’s timezone with the default format.

$utc_timestamp = 1623096269;
$utc_timestamp_converted = date( 'Y-m-d H:i:s', $utc_timestamp );
$local_timestamp = get_date_from_gmt( $utc_timestamp_converted );

Convert a Unix timestamp to site’s timezone with custom format

Convert a Unix timestamp to the site’s timezone with a custom format.

$utc_timestamp = 1623096269;
$utc_timestamp_converted = date( 'Y-m-d H:i:s', $utc_timestamp );
$format = 'm/d/Y H:i';
$local_timestamp = get_date_from_gmt( $utc_timestamp_converted, $format );

Display a post’s date in site’s timezone

Display the date of a post in the site’s timezone with a custom format.

global $post;
$utc_date = $post->post_date_gmt;
$format = 'F j, Y, g:i a';
$local_date = get_date_from_gmt( $utc_date, $format );
echo $local_date;