如何实现类似facebook的时间戳差值功能

发布于 2024-12-02 18:44:44 字数 1049 浏览 1 评论 0原文

我正在使用 php 设计一个类似 facebook 的时间戳差异函数,如下所示:

function time_parser($time)
    {

        if(!is_numeric($time))
        {
            $time = strtotime($time);
            if(!is_numeric($time))
            {
                return "";
            }
        }
        $difference = time() - $time;
        $periods = array("sec","min","hour","day","week","month","year");
        $lengths = array("60","60","24","7","4.35","12","10");

        if($difference > 0)
        {
            $ending = "ago";
        }
        else
        {
            $difference = -$difference;
            $ending = "to go";
        }
        for($j=0; $difference>=$lengths[$j] && $j<7;$j++)
        {
            $difference /= $lengths[$j];
        }
        $difference = round($difference);
        if($difference!=1)
        {
            $periods[$j].="s";
        }
                    $text = "$difference $periods[$j] $ending";
        return $text;
    }

现在的问题是,它给出的结果是一分钟前的时间,以小时为单位。怎么了 ?这取决于我电脑的时区设置吗?

I am designing a facebook like timestamp difference function using php as shown below :

function time_parser($time)
    {

        if(!is_numeric($time))
        {
            $time = strtotime($time);
            if(!is_numeric($time))
            {
                return "";
            }
        }
        $difference = time() - $time;
        $periods = array("sec","min","hour","day","week","month","year");
        $lengths = array("60","60","24","7","4.35","12","10");

        if($difference > 0)
        {
            $ending = "ago";
        }
        else
        {
            $difference = -$difference;
            $ending = "to go";
        }
        for($j=0; $difference>=$lengths[$j] && $j<7;$j++)
        {
            $difference /= $lengths[$j];
        }
        $difference = round($difference);
        if($difference!=1)
        {
            $periods[$j].="s";
        }
                    $text = "$difference $periods[$j] $ending";
        return $text;
    }

Now the problem is , it gives results in hours for a time which is a minute ago . What is wrong ? Does it depend on the timezone settings of my pc?

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

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

发布评论

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

评论(1

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