如果我禁用錯誤,此功能正常工作。但我必須解決它們。 所以我啓用了錯誤,並發現了以下錯誤:錯誤:未定義的變量:retval和未定義的常量長度 - 假定'長度'
Notice: Undefined variable: retval in /home/content/79/9482579/html/gamelinkexchange.com/all/function.php on line 20
Notice: Use of undefined constant length - assumed 'length' in /home/content/79/9482579/html/gamelinkexchange.com/all/function.php on line 55
這裏是我的功能:
function time_ago($date,$granularity=2) {
date_default_timezone_set('Asia/Calcutta');
$date = strtotime($date);
$difference = time() - $date;
$periods = array('decade' => 315360000,
'year' => 31536000,
'month' => 2628000,
'week' => 604800,
'day' => 86400,
'hour' => 3600,
'minute' => 60,
'second' => 1);
foreach ($periods as $key => $value) {
if ($difference >= $value) {
$time = floor($difference/$value);
$difference %= $value;
$retval .= ($retval ? ' ' : '').$time.' '; /*error*/
$retval .= (($time > 1) ? $key.'s' : $key);
$granularity--;
}
if ($granularity == '0') { break; }
}
return $retval.' ago';
}
function gethours($str)
{
if(strpos($str, "decade"))
{
return 0;
}
else if(strpos($str, "year"))
{
return 0;
}
else if(strpos($str, "month"))
{
return 0;
}
else if(strpos($str, "week"))
{
return 0;
}
else if(strpos($str, "day"))
{
return 0;
}
else if(strpos($str, "hours"))
{
$strarr= explode(" ",$str);
$j=0; /*error*/
for($i=0;$i<$strarr.length;$i++)
{
if($strarr[$i]=="hours")
{
$j=$i-1;
}
}
return $strarr[$j];
}
else { return 1; }
}
此功能運行速度非常慢,所以我需要解決這些錯誤。我嘗試了本網站和其他論壇的其他頁面提供的解決方案,但沒有任何幫助。請提供一個消耗較少資源的好解決方案。
我需要一個解決方案。因爲每個條目都必須消磨時間。
這是我的第一個問題,我是新手,請大家幫忙。謝謝。
這實在不是一個 「做我的工作對我來說」 的網站,這個問題似乎特別適合於爲你完成工作,而不是提出一個對未來訪問者有用的問題。 – cdhowie