2014-11-11 2 views
0

나는 모든 날짜를 UTC 타임 스탬프로 저장하는 웹 응용 프로그램이 있습니다. 시간대 설정을 사용하여 클라이언트의 표시 목적으로 날짜가 변경됩니다. 그러나 지난 주 일요일 (2014 년 11 월 2 일) DST가 미국에서 끝났을 때 최악의 경우를 맞았습니다. strtotime과 "+1 Day"를 사용하기 때문에 코드가이 코드를 처리한다고 생각했지만 그렇지 않습니다.PHP에서 타임 스탬프에 1 일 추가

$current_date_start=$this->date_start; //$this->date_start is a UTC timestamp 
$current_date_end=strtotime('+1 day', $current_date_start); 
do 
{ 
    $current_date_start=strtotime('+1 day', $current_date_start); 
    $current_date_end=strtotime('+1 day', $current_date_start); 
    echo format_local_date($current_date_start,'America/Los_Angeles',"D F j Y H i s")."<br />"; 
} 
while ($current_date_start<$this->date_end);  


function format_local_date($timestamp,$timezone,$format_str='') 
{ 
    $date_time=new DateTime_52("now",new DateTimeZone($timezone)); 
    $date_time->setTimestamp($timestamp); 
    if ($format_str=='') 
     $format_str="F j Y"; 
    return $date_time->format($format_str); 
} 

// 
//DateTime_52 class 
// 

/** 
* Provides backwards support for php 5.2's lack of setTimestamp and getTimestamp 
*/ 
class DateTime_52 extends DateTime{ 
    /** 
    * Set the time of the datetime object by a unix timestamp 
    * @param int $unixtimestamp 
    * @return DateTime_52 
    */ 
    public function setTimestamp($unixtimestamp){ 
     if(!is_numeric($unixtimestamp) && !is_null($unixtimestamp)){ 
      trigger_error('DateTime::setTimestamp() expects parameter 1 to be long, '.gettype($unixtimestamp).' given', E_USER_WARNING); 
     } else { 
      $default_timezone=date_default_timezone_get(); 
      $this_timezone= $this->getTimezone(); 
      date_default_timezone_set($this->getTimezone()->getName()); 
      $this->setDate(date('Y', $unixtimestamp), date('n', $unixtimestamp), date('d', $unixtimestamp)); 
      $this->setTime(date('G', $unixtimestamp), date('i', $unixtimestamp), date('s', $unixtimestamp)); 
      date_default_timezone_set($default_timezone); 
     } 
     return $this; 
    } 
    /** 
    * Get the time of the datetime object as a unix timestamp 
    * @return int a unix timestamp representing the time in the datetime object 
    */ 
    public function getTimestamp(){ 
     return $this->format('U'); 
    } 
} 

을 그리고 여기 출력은 다음과 같습니다 : 여기에 코드를 내가있어 그

Sun November 2 2014 00 00 00 
Sun November 2 2014 23 00 00 
Mon November 3 2014 23 00 00 
Tue November 4 2014 23 00 00 
Wed November 5 2014 23 00 00 
Thu November 6 2014 23 00 00 
Fri November 7 2014 23 00 00 
Sat November 8 2014 23 00 00 

당신은 한 시간 떨어진 것 볼 수 있듯이. 분명히 DST 엣지 케이스. 하지만 "+1 일"이 그것을 처리해야한다고 생각했습니다. 도움!

답변

1

작업 순서가 잘못되었습니다. DST 변동이없는 UTC 시간에 +1 일을 추가하고 있습니다. 로컬 시간대로 먼저 변환해야합니다. 그 다음에 +1 일을 더하면 더할 나위없이 계산이 정확한 시간대 내에 있기 때문에 예상대로 자동으로 확인됩니다.

+0

감사합니다. 정확한 방향으로 나를 지적했는데 모든 것이 완벽하게 작동합니다! – nemmy

관련 문제