PHP中的X时间

PHP中的X时间,php,timestamp,Php,Timestamp,我找到了一个类,用于将时间戳转换为X时间。它工作得很好,除了一个问题。我在太平洋时区,所以时间总是在8小时前,而实际上应该是2秒前 class Cokidoo_DateTime extends DateTime { protected $strings = array( 'y' => array('1 year ago', '%d years ago'), 'm' => array('1 month ago', '%d months ago')

我找到了一个类,用于将时间戳转换为X时间。它工作得很好,除了一个问题。我在太平洋时区,所以时间总是在8小时前,而实际上应该是2秒前

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;
    }
}
默认设置为当前_时间戳

如何修改我的方法以使其工作?理想情况下,我希望这对每个人都是通用的,所以无论你在哪个时区,它都会根据新条目存在的时间显示X个时间之前


我相信这与UTC有关?

如果在构建
Cokidoo\u DateTime
时正确设置时区,在方法
\u\u toString()
$now=new DateTime('now')中应从父级获取时区:

$now = new DateTime('now', $this->getTimezone());
设置时区:

$now = new DateTime('now', $this->getTimezone());