如果我禁用错误,此功能正常工作。但我必须解决它们。 所以我启用了错误,并发现了以下错误:错误:未定义的变量: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