You don't have to change the server timezone and determine if the DST is currently effective. Here is a easy way to do it:
function isDst($timezone) {
$date = new DateTime('now', new DateTimeZone($timezone));
return (bool) $date->format('I');
}
// isDst(date_default_timezone_get()) for server timezone
// isDst('America/Los_Angeles')
To determine the time difference between two time zones:
function getTimezoneDiff($tz1, $tz2) {
$date1 = new DateTime('today', new DateTimeZone($tz1));
$date2 = new DateTime('today', new DateTimeZone($tz2));
// Get the difference in seconds
$secondsDiff = $date2->getTimestamp() - $date1->getTimestamp();
// Some magic to format the time difference
$timeDiff = gmdate('H:i:s', abs($secondsDiff));
return (($secondsDiff > 0) ? '' : '-') . $timeDiff;
}
// getTimezoneDiff('America/Los_Angeles', 'Asia/Kolkata') ==> '-12:30:30'
Note: A negative return value indicates $tz1
is behind $tz2
.
If you have lot of date time manipulations in your app you can use the Carbon library, which makes it a breeze to do the date time calculations.