我在我的网站上发帖并希望将时间显示为“3分钟前”
但这是我的功能无法正常工作。
发布更新的mysql timestamp
作为$time
传递,如2018-04-15 09:00:02
这个帖子让时间只是一些前面的但是在功能处理后的页面上显示9 Hours ago
请帮助我显示正确的TimeAgo
public static function timeago($time){
$time=strtotime($time);
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$difference = $now - $time;
$tense= "ago";
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1) {
$periods[$j].= "s";
}
return "$difference $periods[$j] $tense ";
}
答案 0 :(得分:0)
你的问题是你没有为时间戳值写一个语句, 我的意思是你在函数中使用的每个值都被认为是时间戳而不是时间字符串。
public static function timeago($time){
if (!is_numeric($time) || (int)$time != $time)
$time=strtotime($time);
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$difference = $now - $time;
$tense= "ago";
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1) {
$periods[$j].= "s";
}
return "$difference $periods[$j] $tense ";
}
顺便说一句,我很抱歉我的英语,这不是我的母语。