转换时间戳的时区差异

发布于 2024-12-14 12:50:13 字数 1953 浏览 1 评论 0原文

我找到了这个类,用于将时间戳转换为 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;
        }
    }

如何通过 SQL 插入新行:

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

默认设置为 CURRENT_TIMESTAMP;

我该如何修改我的方法才能使其发挥作用? 理想情况下,我希望这对每个人来说都是通用的,因此无论您位于哪个时区,它都会根据新条目的存在时间显示 X 时间之前。

I've got this class I found for converting timestamps to X Time Ago.
It works great, except for one problem. I'm in the Pacific Time Zone, and so the time always says 8 hours ago, when it should really say 2 seconds ago.

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

How I insert new rows via SQL:

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

Default is set to CURRENT_TIMESTAMP;

How can I modify my method to get this to work?
Ideally, I'd like this to be universal for everyone, so no matter what time zone you're in, it will shows X time ago based on when a new entry is in existence.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与他有关 2024-12-21 12:50:13
        $now = new DateTime('now');

请求当地时间。但你想要 UTC 时间。 指定 UTC 作为构造函数的第二个参数。

        $now = new DateTime('now');

This requests local time. But you want UTC time. Specify UTC as a second parameter to the constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文