我已经查看了SO上的其他解决方案,但似乎没有解决其中的timezone / dst问题 以下方面。</ p>

我打电话给 NOAA 潮汐预测API NOAA国家气象服务API ,需要时间范围 被传递以检索数据。 对于我的数据库中的每个位置,我将时区作为UTC偏移量以及是否观察到夏令时(1或0)。 我正在尝试将某些日期(今天和明天)格式化为LST(本地标准时间)将在其自己的时区中,因此我可以传递给这些API。</ p>

我' 我无法弄清楚如何知道某个日期,例如今天,是否在夏令时范围内。</ p>

这是我到目前为止所做的:</ p> \ n

//注意:$ locationdata-&gt;时区类似于“-5”
$ tz_name = timezone_name_from_abbr(“”,$ locationdata- &gt; timezone * 3600,false);
$ dtz = new DateTimeZone($ tz_name);

$ start_time = new DateTime('',$ dtz);
$ end_time = new DateTime('',$ dtz);
$ end_time = $ end_time-&gt; modify ('+1天');

$ start_time-&gt; setTimezone($ dtz);
$ end_time-&gt; setTimezone($ dtz);

if($ locationdata-&gt; dst =='1')

// ** **我如何知道今天是否是当前的DST? **


</ code> </ pre>

我该如何进行此操作 ? 谢谢。</ p>
</ div>



I've looked through the other solutions on SO and none of them seem to address the timezone/dst issue in the following regard.

I am making calls to NOAA Tide Prediction API and NOAA National Weather Service API which require a time range to be passed for retrieving data. For each location in my database, I have the timezone as a UTC offset and whether daylight savings time is observed (either 1 or 0). I'm trying to format some dates (todays and tomorrow) to be what the LST (Local Standard Time) would be in it's own timezone so I can pass to these API's.

I'm having trouble figuring out how to know if a date, such as todays, is within the daylight savings time range or not.

Here is what I have so far:

// Get name of timezone for tide station
// NOTE: $locationdata->timezone is something like "-5"
$tz_name = timezone_name_from_abbr("", $locationdata->timezone * 3600, false);
$dtz = new DateTimeZone($tz_name);    

// Create time range
$start_time = new DateTime('', $dtz);
$end_time = new DateTime('', $dtz);
$end_time = $end_time->modify('+1 day');

// Modify time to match local timezone

// Adjust for daylight savings time
if( $locationdata->dst == '1' )
   // DST is observed in this area. 



// Make call to API using modified time range

How can I go about doing this? Thanks.


您可以使用PHP的时间和日期函数:</ p>

  $ tzObj = timezone_open(  $ tz_name); 
$ dateObj = date_create(“07.03.2012 10:10:10”,$ tzObj);

$ dst_active = date_format($ dateObj,“I”);
</ code> < / pre>

如果DST在给定日期处于活动状态, $ dst_active </ code>为 1 </ code>,否则 0 </ code>。</ p>

您也可以通过“now”</ code>来接收当前日期和时间的值,而不是在 date_create </ code>的调用中指定时间。 。</ p>

但是,如同Jon所提到的,同一时区内的不同国家/地区可能会发现DST,而其他国家可能没有。</ p>
</ div>



You can use PHP's time and date functions:

$tzObj = timezone_open($tz_name);
$dateObj = date_create("07.03.2012 10:10:10", $tzObj);

$dst_active = date_format($dateObj, "I");

If DST is active on the given date, $dst_active is 1, else 0.

Instead of specifying a time in the call to date_create you can also pass "now" to receive the value for the current date and time.

However, like Jon mentioned, different countries within the same timezone offset may observe DST while others may not.

doudie2693 ^我的上帝.........
7 年多之前 回复
drh96824 但是,请注意这些问题。 bugs.php.net/bug.php?id=40743 bugs.php.net/bug?php?id=49914 bugs.php.net/bug.php?id=51051 bugs.php.net/bug.php?id = 51557 bugs.php.net/bug.php?id=52480 bugs.php.net/bug.php?id=54340 bugs.php.net/bug.php?id=54655 bugs.php.net/bug.php ?id = 55253 bugs.php.net/bug.php?id=60873 bugs.php.net/bug.php?id=60960 bugs.php.net/bug.php?id=61022 bugs.php.net/bug .php?id = 61311 bugs.php.net/bug.php?id=61530 bugs.php.net/bug.php?id=61955
大约 8 年之前 回复

对于我数据库中的每个位置,我将时区作为UTC偏移量以及是否观察到夏令时( 1或0)。</ p>
</ blockquote>

这些信息不够。 可以有多个时区都具有相同的标准偏移,都观察到DST,但是在不同时间执行DST转换。 (实际上,从历史上看,它们也可以开始和停止观察夏令时数年。)</ p>

基本上,您的数据库应该</ em>包含时区ID,而不是 偏移/ DST-真或假。 (假设PHP使用zoneinfo时区数据库,时区ID类似于“Europe / London”。)</ p>

编辑:查找给定 DateTime的偏移量</ 代码>,您可以致电 getOffset </ code> , 然后,您可以将其与标准时间偏移进行比较。 但除非您拥有明确的</ em>时区ID,否则将</ em>冒险获取错误的区域。</ p>
</ div>



For each location in my database, I have the timezone as a UTC offset and whether daylight savings time is observed (either 1 or 0).

That's not enough information. There can be multiple time zones which all have the same standard offset, all observe DST, but perform DST transitions at different times. (Indeed, historically they may also start and stop observing daylight saving time for several years.)

Basically, your database should contain a time zone ID, not the offset/DST-true-or-false. (Assuming PHP uses the zoneinfo time zone database, a time zone ID is something like "Europe/London".)

EDIT: To find the offset of a given DateTime, you can call getOffset, which you can then compare with the standard time offset. But unless you have the definitive time zone ID, you will be risking getting the wrong zone.

dpsx99068 您可以使用cron作业使tzdata(以及DST)定义保持最新,以便经常更新此PECL包:pecl.php.net/package/timezonedb
大约 8 年之前 回复
dongyu1983 嗯,很难知道如果你不知道你真正感兴趣的区域......但正如我所说,你可以在DateTime上调用getOffset来查找。
8 年多之前 回复
我在西湖1 我确信会有歧义,但此刻我并不担心。 我更关心的是确定一个区域目前是否正在观察夏令时。
8 年多之前 回复
dongyaoxiu6244 您是否依赖该映射是准确的? 因为它会含糊不清。 虽然会编辑回答具体问题...
8 年多之前 回复
dongqian3198 函数timezone_name_from_abbr(“”,$ locationdata-> timezone * 3600,false); 从偏移返回时区ID,因此-5可能会返回'America / New York`。
8 年多之前 回复

我希望你不使用Java! 我和时间一直在战斗。 我也使用天气数据。 我使用的大部分数据都是在本地标准时间内(忽略夏令时)。 我还需要使用其他时区的时间,发现Java一直在读取计算机的时区。 我也一直在运行已弃用的课程。 我提出了一个有效的解决方案。 它有点像kluge,所以我有大量记录,它只存在于一个函数中。 我的解决方案是相对时间。 我已将当地时间设置为UTC。 我正在减去GMT偏移而不是添加它。 我真的不关心实际的时间,我关心的是两次之间的区别。 它工作得非常好。
祝你好运</ p>
</ div>



Cillosis, I hope you are not working with Java! I am and fought with time all the time. I also work with weather data. Most of the data I use is in local standard time (ignoring daylight saving time). I also need to use times from other time zones and found that Java kept reading my computer's time zone. I also kept running into deprecated classes. I came up with a solution that works. It is a bit of a kluge, so I have it heavily documented and it only exists in one function. My solution is relative time. I have set the local time to UTC. I am subtracting the GMT offset instead of adding it. I don’t really care about the actual times, all I care about is the difference between two times. It is working very well. Good luck

dongyuan2388 我正在使用PHP并最终实现了类似的解决方案。
8 年多之前 回复
Csdn user default icon