dsuvs66406
2011-06-01 13:59
浏览 82
已采纳

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

From 5.3 the is_dst parameter is deprecated in mktime. But I need two previous time in timestamp, one of them with dst and the other without that.

E.g.: first time mktime("03","15","00","08","08","2008",1) and the other mktime("03","15","00","08","08","2008",0)

Can you help me to solve that problem?

图片转代码服务由CSDN问答提供 功能建议

来自5.3 is_dst 参数在 mktime 中已弃用。 但是我需要前两个时间戳,其中一个是dst而另一个没有。

例如:第一次 mktime(“03”,“15”,“00 “,”08“,”08“,”2008“,1)和其他 mktime(”03“,”15“,”00“,”08“,”08“,”2008 “,0)

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dstm2014 2011-06-01 14:33
    已采纳

    Not a full answer, just some ideas I could think of... The DateTime::format() method accepts a format code that tells whether DST is on effect:

    $now = new DateTime;
    $is_dst = $now->format('I')==1;
    

    Now, in order to know what time it'd be if DST was the other way round, you have to find out when such change happens. This code:

    $time_zone = $now->getTimeZone();
    var_dump( $time_zone->getTransitions(strtotime('2011-01-01'), strtotime('2011-12-31')) );
    

    ... prints:

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

    If you obtain the transitions for year the date belongs to, you can collect a list of offsets according for isdst TRUE and isdst FALSE. Once you pick an appropriate offset, the rest is easy:

    $winter_offset = 3600;
    $summer_offset = 7200;
    $difference = $winter_offset-$summer_offset;
    $winter = $now->modify( ($difference<0 ? '' : '+') . $difference . ' seconds');
    echo $winter->format('H:i:s');
    
    点赞 打赏 评论
  • dsxon40042 2011-06-01 14:13

    I have found this interesting answer on 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.
    */
    


    View the source.

    点赞 打赏 评论

相关推荐 更多相似问题