如何实现facebook的时间戳差分功能


How to implement facebook like timestamp difference function

我正在使用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;
    }

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

使用JavaScript进行时间转换比使用PHP要好。

http://tinywall.info/2011/10/09/facebook-like-friendly-time-with-gmt-and-utc-in-web-application-with-php-javascript/