Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get time zone offset between two timezones for a given duration

I wanted to know if there is a way to get the time zone offset for a given date range between two timezones for a given duration.

getTimezoneOffset(startDate,endDate,timezone1,timezone2){
    ...missing magic to go here...
}

should return the time zone offset which is valid for the a given duration. However if the offset changes, it should return the date range for which it's valid.

So I am looking at something like this:

getTimezoneOFfset("march 9 12 am", "march 15 12 am", "UTC", "US/NEW_YORK")

return value something like this

timezoneoffset[0]["range"]=[march 9 12am to march 11 2 am]
timezoneoffset[0]["offset"]=5
timezoneoffset[1]["range"]=[march 9 2 am to march 15 12 am]
timezoneoffset[1]["offset"]=4

I just don't want to calculate timezone offsets for every scheduled item for the given range. Was looking if there is some way to get a direct lookup for offsets if it's going to change.

I am working with PHP, but a solution in any language will be appreciated.

MySQL solution will also work as it will be more optimized to do this in MySQL.

like image 600
user1192612 Avatar asked Feb 06 '12 15:02

user1192612


People also ask

How do you find time zone offset?

getTimezoneOffset() returns the difference between UTC time and local time. getTimezoneOffset() returns the difference in minutes. For example, if your time zone is GMT+2, -120 will be returned.

How does time offset work?

A UTC offset is the difference in hours and minutes between a particular time zone and UTC, the time at zero degrees longitude. For example, New York is UTC-05:00, which means it is five hours behind London, which is UTC±00:00.

How many timezone offsets are there?

Time zones A UTC offset is the difference in time between UTC time and a location's observed time. There are currently 38 observed UTC offsets in the world (37 when Iran is on daylight saving time).

How do you divide time zones?

The Earth is loosely divided into 24 regions (time zones) separated by longitude. Not counting local variations, each line of longitude is divided by fifteen degrees; as a general rule and depending upon which way one travels, time moves forward or backward one hour for every fifteen degrees of longitude.


2 Answers

Assuming that you have a newer version of php (5.2.0+) the DateTimeZone class is part of the PHP core and will let you use the getOffset() function to make these calculations. It will let you pass in a dateTime object and specify a timezone. If you do this for both your dates you should be able to calculate all of the pieces you're looking to grab. To use an example from php.net:

// Create two timezone objects, one for Taipei (Taiwan) and one for
// Tokyo (Japan)
$dateTimeZoneTaipei = new DateTimeZone("Asia/Taipei");
$dateTimeZoneJapan = new DateTimeZone("Asia/Tokyo");

// Create two DateTime objects that will contain the same Unix timestamp, but
// have different timezones attached to them.
$dateTimeTaipei = new DateTime(mktime([the date in question]), $dateTimeZoneTaipei);
$dateTimeJapan = new DateTime(mktime([the date in question]), $dateTimeZoneJapan);


// Calculate the GMT offset for the date/time contained in the $dateTimeTaipei
// object, but using the timezone rules as defined for Tokyo
// ($dateTimeZoneJapan).
$timeOffset = $dateTimeZoneJapan->getOffset($dateTimeTaipei);

// Should show int(32400) (for dates after Sat Sep 8 01:00:00 1951 JST).
print("Number of seconds Japan is ahead of GMT at the specific time: ");
var_dump($timeOffset);

print("<br />Number of seconds Taipei is ahead of GMT at the specific time: ");
var_dump($dateTimeZoneTaipei->getOffset($dateTimeTaipei));

print("<br />Number of seconds Japan is ahead of Taipei at the specific time: ");
var_dump($dateTimeZoneJapan->getOffset($dateTimeTaipei)
     -$dateTimeZoneTaipei->getOffset($dateTimeTaipei));
like image 173
Ben D Avatar answered Oct 13 '22 10:10

Ben D


This working for me:

function Get_Timezone_Offset($remote_tz, $origin_tz = null) 
{
    if($origin_tz === null) 
    {
        if(!is_string($origin_tz = date_default_timezone_get())) {
            return false;
        }
    }
    $origin_dtz = new DateTimeZone($origin_tz);
    $remote_dtz = new DateTimeZone($remote_tz);
    $origin_dt  = new DateTime("now", $origin_dtz);
    $remote_dt  = new DateTime("now", $remote_dtz);
    $offset     = $origin_dtz->getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt);
    return $offset;
}

To use it

echo Get_Timezone_Offset('America/New_York', 'Europe/Stockholm');

Source: http://php.net/manual/en/function.timezone-offset-get.php

like image 23
Mohamad Hamouday Avatar answered Oct 13 '22 11:10

Mohamad Hamouday