PHP daylight saving time detection

前端 未结 3 1682
暗喜
暗喜 2020-11-28 09:15

I need to send an email to users based wherever in the world at 9:00 am local time. The server is in the UK. What I can do is set up a time difference between each user and

3条回答
  •  温柔的废话
    2020-11-28 10:00

    As Jimmy points out you can use timezone transitions, but this is not available on PHP <5.3. as dateTimeZone() is PHP>=5.2.2 but getTransitions() with arguments is not! In that case here is a function that can give you timezone data, including whether in DST or not.

    function timezonez($timezone = 'Europe/London'){
        $tz = new DateTimeZone($timezone);
        $transitions = $tz->getTransitions();
        if (is_array($transitions)){
            foreach ($transitions as $k => $t){
                // look for current year
                if (substr($t['time'],0,4) == date('Y')){
                    $trans = $t;
                    break;
                }
            }
        }
        return (isset($trans)) ? $trans : false;
    }
    

    Having said that, there is a simpler method using date() if you just need to know whether a timezone is in DST. For example if you want to know if UK is in DST you can do this:

    date_default_timezone_set('Europe/London');
    $bool = date('I'); // this will be 1 in DST or else 0
    

    ... or supply a timestamp as a second arg to date() if you want to specify a datetime other than your current server time.

提交回复
热议问题