在PHP 5.3中使用mktime()中的is_dst(不推荐使用参数)

时间:2011-06-01 13:59:31

标签: php date time dst mktime

从5.3 the is_dst parameter is deprecated in mktime开始。但我之前需要两个时间戳,其中一个是dst而另一个没有。

例如:第一次mktime("03","15","00","08","08","2008",1)和另一个mktime("03","15","00","08","08","2008",0)

你能帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

我在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.
*/


查看source

答案 1 :(得分:0)

不是一个完整的答案,只是我能想到的一些想法...... 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');