Created
November 23, 2016 19:51
-
-
Save pbearne/9b893108ccebe4c4783eefce0ee9f5a9 to your computer and use it in GitHub Desktop.
wordpress time offset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$original_timezone = new DateTimeZone( 'UTC' ); | |
// Instantiate the DateTime object, setting it's date, time and time zone. | |
$datetime = new DateTime( $time_to_change, $original_timezone ); | |
$blog_timezone = get_option( 'timezone_string' ); | |
// timezone not set use offset | |
if ( ! empty( $blog_timezone ) ) { | |
// Set the DateTime object's time zone to convert the time appropriately. | |
$target_timezone = new DateTimeZone( $blog_timezone ); | |
} else { | |
$offset = get_option( 'gmt_offset' ); | |
$sign = $offset < 0 ? '-' : '+'; | |
$target_timezone = new DateTimeZone( $sign . abs( $offset ) ); | |
} | |
$datetime->setTimezone( $target_timezone ); | |
$new_time = $datetime->format( $format ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment