2011-01-29 8 views

답변

4

저는 PHP 사용자가 아니지만 가능한 것처럼 보입니다.하지만 어색합니다.

DateTimeZone이 있으면 현재 오프셋 과 전환 세트을 찾을 수 있습니다. 따라서 "지금"에서 전환을 요청하면 현재 시간대에 대한 정보를 찾을 수 있습니다. the docs에서 약간 변형 예 : 그래서 여기에 PHP 내 C++ 시간대 코드의 빠른 포트입니다,

$theTime = time(); # specific date/time we're checking, in epoch seconds. 

$tz = new DateTimeZone('America/Los_Angeles'); 
$transition = $tz->getTransitions($theTime,$theTime); 

# only one array should be returned into $transition. 
$dst = $transition[0]['isdst']; 
+0

C에서 적어도 is_dst는 ** 잘못된 **입니다. 시간대에 일광 절약 시간제가 적용되는지 여부는 알려주지 만 반드시 필요하지는 않습니다. – Mikel

+0

@Mikel : 시간대를 묻지 않습니다. 나는 현재 적용되고있는 전환 *을 요구하고있다. (또는 이전의 전환 결과, 현재 상태입니다.) –

+0

실제로 FreeBSD 및 Linux/glibc처럼 보이지만 is_dst는 일광 절약 시간이 지정된 시간에 적용될 경우에만 1이므로 귀하가 내 의견을 무시할 수 있다면. 나는 그것이 모든 시스템에서 이식 가능하지 않다는 것을 기억합니다. – Mikel

2

글쎄, 그것은 옳은 일을하도록되어 isdst처럼 들리 겠지만 내가 전에 물린했습니다

// given a timezone and a timestamp 
// return true if timezone has DST at timestamp, false otherwise 
// timezone defaults to current timezone 
// timestamp defaults to now 
function is_dst($timezone = null, $time = null) { 
    $oldtimezone = date_default_timezone_get(); 
    if (isset($timezone)) { 
     date_default_timezone_set($timezone); 
    } 

    if (!isset($time)) { 
     $time = time(); 
    } 

    $tm = localtime($time, true); 
    $isdst = $tm['tm_isdst']; 
    $offset = 0; 
    //$dsttime = mktime_array($tm); 
    //echo strftime("%c", $dsttime); 

    $tm['tm_isdst'] = 0; 
    $nondsttime = mktime_array($tm); 
    $offset = $nondsttime - $time; 

    date_default_timezone_set($oldtimezone); 

    return $offset != 0; 
} 

function mktime_array($tm) { 
    return mktime($tm['tm_hour'], $tm['tm_min'], $tm['tm_sec'], $tm['tm_mon']+1, $tm['tm_mday'], $tm['tm_year']+1900, isset($tm['tm_isdst'])? $tm['tm_isdst']: -1); 
} 

그리고 당신이 사용할 수있는 몇 가지 코드를 테스트 : 나를 위해

foreach (array(null, "Australia/Sydney", "UTC", "America/Los_Angeles") as $tz) { 
    $isdst = is_dst($tz); 
    if (isset($tz)) { 
     echo $tz; 
    } 
    else { 
     echo "current timezone"; 
    } 
    echo " "; 
    if ($isdst) { 
     echo "has daylight savings now\n"; 
    } 
    else { 
     echo "has standard time now\n"; 
    } 
} 

// tests based on known transitions for Sydney (AEST) 
foreach (array(null, "2011-04-03 01:59:00", "2011-04-03 02:00:00", "2011-10-02 01:59:00", "2011-10-02 03:00:00") as $timestr) { 
    $tz = "Australia/Sydney"; 
    if (isset($timestr)) { 
     $tm = strptime($timestr, "%Y-%m-%d %H:%M:%S"); 
     $time = mktime_array($tm); 
    } 
    else { 
     $time = time(); 
    } 
    $isdst = is_dst($tz, $time); 
    if (isset($tz)) { 
     echo $tz; 
    } 
    else { 
     echo "current timezone"; 
    } 
    echo " "; 
    if ($isdst) { 
     echo "has daylight savings at $timestr\n"; 
    } 
    else { 
     echo "has standard time at $timestr\n"; 
    } 
} 

를, 그것은 인쇄 :

current timezone has daylight savings now 
Australia/Sydney has daylight savings now 
UTC has standard time now 
America/Los_Angeles has standard time now 
Australia/Sydney has daylight savings at 
Australia/Sydney has daylight savings at 2011-04-03 01:59:00 
Australia/Sydney has standard time at 2011-04-03 02:00:00 
Australia/Sydney has standard time at 2011-10-02 01:59:00 
Australia/Sydney has daylight savings at 2011-10-02 03:00:00 
관련 문제