在给定的持续时间内获取两个时区之间的时区偏移 [英] Get time zone offset between two timezones for a given duration

查看:173
本文介绍了在给定的持续时间内获取两个时区之间的时区偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在给定的持续时间内是否有两种时区之间的给定日期范围的时区偏移的方法。

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")

返回值这样的

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.

我正在使用PHP,但任何语言的解决方案都将不胜感激。

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

MySQL解决方案也将起作用,因为它将在MySQL中进行更优化。

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

推荐答案

假设你有一个较新版本的php(5.2.0+), DateTimeZone 类是PHP内核的一部分,并允许您使用 getOffset()函数进行这些计算。它将让您传递 dateTime对象并指定时区。如果你这两个日期都这样做,你应该可以计算出你想要抓取的所有部分。要使用php.net中的一个例子:

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));

这篇关于在给定的持续时间内获取两个时区之间的时区偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆