2011-11-09 2 views
0

타임 스탬프를 X 시간 전으로 변환 한이 클래스를 찾았습니다. 하나의 문제를 제외하고는 훌륭하게 작동합니다. 저는 태평양 표준 시간대에 있습니다. 그래서 시간은 항상 2 시간 전인 정말로 8 시간 전이라고합니다. 나는 SQL을 통해 새로운 행을 삽입하는 방법변환 된 타임 스탬프의 시간대 차이

class Cokidoo_DateTime extends DateTime { 
     protected $strings = array(
      'y' => array('1 year ago', '%d years ago'), 
      'm' => array('1 month ago', '%d months ago'), 
      'd' => array('1 day ago', '%d days ago'), 
      'h' => array('1 hour ago', '%d hours ago'), 
      'i' => array('1 minute ago', '%d minutes ago'), 
      's' => array('now', '%d secons ago'), 
     ); 

     /** 
     * Returns the difference from the current time in the format X time ago 
     * @return string 
     */ 
     public function __toString() { 
      $now = new DateTime('now'); 
      $diff = $this->diff($now); 

      foreach($this->strings as $key => $value){ 
       if(($text = $this->getDiffText($key, $diff))){ 
        return $text; 
       } 
      } 
      return ''; 
     } 

     /** 
     * Try to construct the time diff text with the specified interval key 
     * @param string $intervalKey A value of: [y,m,d,h,i,s] 
     * @param DateInterval $diff 
     * @return string|null 
     */ 
     protected function getDiffText($intervalKey, $diff){ 
      $pluralKey = 1; 
      $value = $diff->$intervalKey; 
      if($value > 0){ 
       if($value < 2){ 
        $pluralKey = 0; 
       } 
       return sprintf($this->strings[$intervalKey][$pluralKey], $value); 
      } 
      return null; 
     } 
    } 

:

mysql_query("INSERT INTO `" . $dbMain . "`.`" . $dbTable . "` (`title`, `date`) VALUES ('$fileTitle', NOW())"); 

기본이 CURRENT_TIMESTAMP로 설정되고,

이 방법이 작동하도록 수정하려면 어떻게해야합니까? 이상적으로 모든 사람이 보편적으로 사용하고 싶습니다. 따라서 어떤 시간대에 있더라도 X 시간 전에 새로운 항목이있는 시점을 기준으로 표시됩니다.

+0

타임 스탬프는 현지 시간 또는 UTC입니까? –

+0

마치 UTC 인 것처럼 보입니다. – Aaron

답변

0
 $now = new DateTime('now'); 

This은 현지 시간을 요청합니다. 하지만 UTC 시간이 필요합니다. Specify UTC를 생성자의 두 번째 매개 변수로 사용합니다.