开发者

Using is_dst in mktime() in PHP 5.3 (parameter is deprecated)

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.: fi开发者_开发技巧rst 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?


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.


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');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜