get_date_from_gmt()
Converts the date from GMT/UTC timezone to the site's timezone. Accepts the date in the format Y-m-d H:i:s.
The return format can be changed in the second parameter, while the passed format must always be Y-m-d H:i:s.
Translates the date based on the site's option timezone_string (example value: Asia/Tashkent). If the option is not set, the option gmt_offset is used (example value: 5, 6, -3, -4...).
To, conversely, get the date in UTC from local, use get_gmt_from_date()
No Hooks.
Returns
String. Date in the current timezone of the site.
Usage
get_date_from_gmt( $date_string, $format );
- $string(string) (required)
- The date that needs to be converted.
- $format(string)
- The format of the returned date. The entire list of possible formats.
Default: 'Y-m-d H:i:s'
Examples
#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
#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. |
get_date_from_gmt() get date from gmt code WP 7.0
function get_date_from_gmt( $date_string, $format = 'Y-m-d H:i:s' ) {
$datetime = date_create( $date_string, new DateTimeZone( 'UTC' ) );
if ( false === $datetime ) {
return gmdate( $format, 0 );
}
return $datetime->setTimezone( wp_timezone() )->format( $format );
}