【问题标题】:Using is_dst in mktime() in PHP 5.3 (parameter is deprecated)在 PHP 5.3 的 mktime() 中使用 is_dst(参数已弃用)
【发布时间】:2011-09-06 08:10:27
【问题描述】:

来自 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)

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

【问题讨论】:

  • 这个要求看起来很奇怪。您真的需要冬季的 DST 时间和夏季的非 DST 时间吗?
  • 有请求,我无法决定是否在 dst 中得到它。之后我想将其转换为时间戳,然后我选择一个日期并将其转换为带有 dst 和不带 dst 的时间戳。我选择那些更接近先前结果的。

标签: php date time dst mktime


【解决方案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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-17
      • 2012-07-24
      • 1970-01-01
      • 2013-08-22
      • 1970-01-01
      • 2014-09-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多