我在我的nginx托管服务器上收到此错误:
Fatal error: TimeElapsedString(): Unknown property (w) in /var/www/script/system/framework.engine.php on line 51
TimeElapsedString:
function TimeElapsedString($datetime, $full = false)
{
date_default_timezone_set('UTC');
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7); // LINE 51
$diff->d -= $diff->w * 7;
$string = array
(
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
);
foreach ($string as $k => &$v)
{
if ($diff->$k)
{
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
}
else
{
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' ago' : 'just now';
}
在本地主机(wamp)上完美运行的一切 托管服务器中的php版本是5.3.3所以我真的没有看到这个问题。 我也在另一个nginx服务器上使用过这个功能,它也没有任何问题。
该功能来自这个答案:Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
答案 0 :(得分:5)
正如@Tularis所说,这是因为你无法在PHP 5.3中为PHP内部对象分配新属性。这是因为DateTime方法diff()返回一个对象,并且无法动态创建新的对象属性。
object(DateInterval)#8 (8) {
["y"]=>
int(0)
["m"]=>
int(6)
["d"]=>
int(11)
["h"]=>
int(9)
["i"]=>
int(45)
["s"]=>
int(10)
["invert"]=>
int(1)
["days"]=>
int(195)
}
要修复它,我将$now->diff($then)
转换为数组,
array(8) {
["y"]=>
int(0)
["m"]=>
int(6)
["d"]=>
int(11)
["h"]=>
int(9)
["i"]=>
int(45)
["s"]=>
int(10)
["invert"]=>
int(1)
["days"]=>
int(195)
}
并使用$ diff作为数组而不是对象来使代码反映更改
// Find how much time has elapsed since now()
// from: https://stackoverflow.com/a/18602474/235633
//
function timeElapsedSinceNow( $datetime, $full = false )
{
$now = new DateTime;
$then = new DateTime( $datetime );
$diff = (array) $now->diff( $then );
$diff['w'] = floor( $diff['d'] / 7 );
$diff['d'] -= $diff['w'] * 7;
$string = array(
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
);
foreach( $string as $k => & $v )
{
if ( $diff[$k] )
{
$v = $diff[$k] . ' ' . $v .( $diff[$k] > 1 ? 's' : '' );
}
else
{
unset( $string[$k] );
}
}
if ( ! $full ) $string = array_slice( $string, 0, 1 );
return $string ? implode( ', ', $string ) . ' ago' : 'just now';
}
顺便说一句,我相信OP的原始问题来自这篇文章,他的问题没有得到答复:https://stackoverflow.com/a/18602474/235633