如果时间少于一定数量,则要显示不同的消息。我有一个功能,我一直在努力,但不能让它工作,除非时间为1。想10分钟。
function TimeSince($timestamp)
{
// array of time period chunks
$chunks = array(
array(60 * 60 * 24 * 365 , 'year'),
array(60 * 60 * 24 * 30 , 'month'),
array(60 * 60 * 24 * 7, 'week'),
array(60 * 60 * 24 , 'day'),
array(60 * 60 , 'hour'),
array(60 , 'minute'),
);
// difference in seconds
$since = time() - $timestamp;
// calculate one chunk of time
for ($i = 0, $j = count($chunks); $i < $j; $i++)
{
$seconds = $chunks[$i][0];
$name = $chunks[$i][1];
// finding the biggest chunk (if the chunk fits, break)
if (($count = floor($since / $seconds)) != 0)
{
break;
}
}
// set output var
$output = ($count == 1) ? '1 '.$name : "$count {$name}s";
// Displays time of if under 10 minutes displays Just Now
if($output < 10) {
return ("Just Now!");
}
else {
return ($output . " ago");
}
return $output;
}
答案 0 :(得分:1)
在以下代码行中:
$output = ($count == 1) ? '1 '.$name : "$count {$name}s";
您将$output
重新定义为字符串,请在将其指定为以下内容之前考虑进行检查:
// Displays time of if under 10 minutes displays Just Now
if($count < 10) {
return ("Just Now!");
}
else {
// set output var
$output = ($count == 1) ? '1 '.$name : "$count {$name}s";
return ($output . " ago");
}
答案 1 :(得分:0)
您将$output
设置为此行中的字符串值:
$output = ($count == 1) ? '1 '.$name : "$count {$name}s";
然后尝试将其与数字进行比较。尝试为$output
分配数值。