Skip to content

Instantly share code, notes, and snippets.

@Emirii
Emirii / getTimezoneOffset.php
Last active June 4, 2024 16:04
PHP Function: Get timezone offset between two timezones in hours
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