Last active
June 14, 2022 19:37
-
-
Save andrewjmead/9aef80a495dc36221ff84ddfb3ac3181 to your computer and use it in GitHub Desktop.
Calculate timezone offsets for PHP & WordPress
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
<?php | |
// This uses the wp option "gmt_offset" as "timezone_string" is not always defined | |
class Timezone | |
{ | |
public static function utc_offset() | |
{ | |
return '+00:00'; | |
} | |
// Will return an offset using the WordPress timezone set by the user | |
// Example return values: -04:00, +00:00, or +5:45 | |
public static function local_offset() | |
{ | |
// Start with the offset such as -4, 0, or 5.75 | |
$offset_number = (float) get_option('gmt_offset'); | |
// Build a string to represent the offset such as -04:00, +00:00, or +5:45 | |
$result = ''; | |
// Start with either - or + | |
$result .= $offset_number < 0 ? '-' : '+'; | |
$whole_part = abs($offset_number); | |
$hour_part = floor($whole_part); | |
$minute_part = $whole_part - $hour_part; | |
$hours = strval($hour_part); | |
$minutes = strval($minute_part * 60); | |
// Add hour part to result | |
$result .= str_pad($hours, 2, '0', STR_PAD_LEFT); | |
// Add separator | |
$result .= ':'; | |
// Add minute part to result | |
$result .= str_pad($minutes, 2, '0', STR_PAD_LEFT); | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment