2011-11-09 7 views
0

タイムスタンプをX Time Agoに変換するためのこのクラスが見つかりました。 これは1つの問題を除いて素晴らしいです。私は太平洋標準時間帯にいるので、時間は常に8時間前と言われています。本当に2秒前です。私は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をコンストラクタの2番目のパラメータとして使用します。