知道为什么这个php函数会导致我的网站冻结吗?
function makeAgo($timestamp){
$difference = time() - $timestamp;
$periods = array("sec", "min", "hr", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
for($j = 0; $difference >= $lengths[$j]; $j++)
$difference /= $lengths[$j];
$difference = round($difference);
if($difference != 1) $periods[$j].= "s";
$text = "$difference $periods[$j] ago";
return $text;
}
答案 0 :(得分:0)
我想你需要改变这个:
for($j = 0; $difference >= $lengths[$j]; $j++){
$difference /= $lengths[$j];
$difference = round($difference);
if($difference != 1) $periods[$j].= "s";
$text = "$difference $periods[$j] ago";
return $text;
}
因为如果你使用没有大括号的循环,只有第一个next语句在循环中连续执行,而不是任何其他语句。
答案 1 :(得分:0)
如果您的时间戳差异小于零,则您的功能会冻结。
还有一件事你忘记提及十年或一年的长度。否则你的功能超级工作...... !!
尝试以下.. !!
function makeAgo($timestamp){
$difference = time() - $timestamp;
if($difference < 0)
{
$difference = 0;
}
$periods = array("sec", "min", "hr", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10","10");
for($j = 0; $difference >= $lengths[$j]; $j++)
$difference /= $lengths[$j];
$difference = round($difference);
if($difference != 1) $periods[$j].= "s";
$text = "$difference $periods[$j] ago";
return $text;
}