从 5.3 开始,该is_dst
参数在mktime
. 但是我在时间戳中需要两个前一个时间,其中一个带有 dst 而另一个没有。
例如:第一次mktime("03","15","00","08","08","2008",1)
和另一个mktime("03","15","00","08","08","2008",0)
你能帮我解决这个问题吗?
我在 Google 上找到了这个有趣的答案:
/*
Since you're getting that error I'll assume you're using PHP 5.1 or
higher. UTC has no concept of DST, so what you really want to be using
are strtotime() and date_default_timezone_set(). Here's the idea:
*/
$someString = '10/16/2006 5:37 pm'; //this is a string
date_default_timezone_set('American/New_York'); //this is the user's timezone. It will determine how the string is turned into UTC
$timestamp = strtotime($someString); //$timestamp now has UTC equivalent of 10/16/2006 5:37 pm in New York
echo date('Y/m/d H:i:s', $timestamp); //prints out the nicely formatted version of that timestamp, as if you were in New York
date_default_timezone_set('American/Los_Angeles');
echo date('Y/m/d H:i:s', $timestamp); //prints out the nicely formatted version of that timestamp, as if you were in LA -- takes care of the conversion and everything
/*
So, setting the timezone then using strtotime() and date() takes care
of all the DST/non-DST stuff, converting between timezones, etc.
*/
查看源代码。
不是一个完整的答案,只是我能想到的一些想法...... DateTime::format()方法接受一个格式代码,该代码告诉 DST 是否生效:
$now = new DateTime;
$is_dst = $now->format('I')==1;
现在,为了知道如果 DST 是相反的时间是什么时候,您必须找出这种变化何时发生。这段代码:
$time_zone = $now->getTimeZone();
var_dump( $time_zone->getTransitions(strtotime('2011-01-01'), strtotime('2011-12-31')) );
... 印刷:
array(3) {
[0]=>
array(5) {
["ts"]=>
int(1293836400)
["time"]=>
string(24) "2010-12-31T23:00:00+0000"
["offset"]=>
int(3600)
["isdst"]=>
bool(false)
["abbr"]=>
string(3) "CET"
}
[1]=>
array(5) {
["ts"]=>
int(1301187600)
["time"]=>
string(24) "2011-03-27T01:00:00+0000"
["offset"]=>
int(7200)
["isdst"]=>
bool(true)
["abbr"]=>
string(4) "CEST"
}
[2]=>
array(5) {
["ts"]=>
int(1319936400)
["time"]=>
string(24) "2011-10-30T01:00:00+0000"
["offset"]=>
int(3600)
["isdst"]=>
bool(false)
["abbr"]=>
string(3) "CET"
}
}
如果您获得日期所属年份的转换,则可以根据 isdst TRUE 和 isdst FALSE 收集偏移列表。一旦你选择了一个合适的偏移量,剩下的就很简单了:
$winter_offset = 3600;
$summer_offset = 7200;
$difference = $winter_offset-$summer_offset;
$winter = $now->modify( ($difference<0 ? '' : '+') . $difference . ' seconds');
echo $winter->format('H:i:s');