2011-11-09 3 views
0

J'ai trouvé cette classe pour convertir les horodatages en X Time Ago. Cela fonctionne très bien, sauf pour un problème. Je suis dans le fuseau horaire du Pacifique, et donc le temps dit toujours il ya 8 heures, quand il devrait vraiment dire 2 secondes.Différences de fuseau horaire dans l'horodatage converti

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; 
     } 
    } 

Comment insérer de nouvelles lignes via SQL:

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

par défaut est réglé sur CURRENT_TIMESTAMP;

Comment puis-je modifier ma méthode pour que cela fonctionne? Idéalement, j'aimerais que ce soit universel pour tout le monde, donc peu importe le fuseau horaire dans lequel vous vous trouvez, il apparaîtra X fois en fonction du moment où une nouvelle entrée existe.

+0

Les horodatages sont-ils en heure locale ou UTC? –

+0

Il semble que ce soit UTC. – Aaron

Répondre

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

This demande l'heure locale. Mais vous voulez l'heure UTC. Specify UTC en tant que deuxième paramètre du constructeur.