Last active
June 4, 2024 16:04
-
-
Save Emirii/6e189002cd84ded8fa89817fad4821a3 to your computer and use it in GitHub Desktop.
PHP Function: Get timezone offset between two timezones in hours
This file contains hidden or 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
function getTimezoneOffset(string $timezone1, string $timezone2): int { | |
$dateTimezone1 = new DateTimeZone($timezone1); | |
$dateTimezone2 = new DateTimeZone($timezone2); | |
$timezoneOffset1 = (new DateTime('now', $dateTimezone1))->getOffset(); | |
$timezoneOffset2 = (new DateTime('now', $dateTimezone2))->getOffset(); | |
return ($timezoneOffset2 - $timezoneOffset1) / 3600; | |
} | |
getTimezoneOffset('America/Chicago', 'America/New_York'); // will return 1 | |
getTimezoneOffset('America/Chicago', 'America/Los_Angeles'); // will return -2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment