如何实现类似facebook的时间戳差值功能
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于时间转换,使用 JavaScript 比使用 PHP 更好。
http://tinywall.info/2011/10/09/facebook-like-friend-time-with-gmt-and-utc-in-web-application-with-php-javascript/
It is better to go with JavaScript than PHP for the time conversion.
http://tinywall.info/2011/10/09/facebook-like-friendly-time-with-gmt-and-utc-in-web-application-with-php-javascript/