未知属性 (w) [英] Unknown property (w)

查看:69
本文介绍了未知属性 (w)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 nginx 托管服务器上收到此错误:

I'm getting this error on my nginx hosted server:

 Fatal error: TimeElapsedString(): Unknown property (w) in /var/www/script/system/framework.engine.php on line 51 

TimeElapsedString:

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 服务器中使用了这个功能,它也没有任何问题.

Everything just working perfect on the localhost (wamp) the php version in the hosted server is 5.3.3 so I ca'nt really see that problem. also I used this function in another nginx server and its also working without any problem.

函数来自这个答案:在 PHP 中将时间戳转换为时间前,例如 1 天前、2 天前...

推荐答案

正如@Tularis 所说,这是因为您无法在 PHP 5.3 中为 PHP 内部对象分配新属性.这是因为 DateTime 方法 diff() 返回一个对象,并且无法动态创建新的对象属性.

As @Tularis said, it's because you can't assign new properties to PHP internal objects in PHP 5.3. This is because the DateTime method diff() returns an object and new object properties can not be created on the fly.

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) 转换成一个数组,

To fix it, I converted $now->diff($then) into an array,

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 作为数组而不是对象来使代码反映更改,

and make the code reflect the changes by using $diff as an array rather than an object,

// 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

BTW, I believe OP's original question comes from this post where his issue went unanswered: https://stackoverflow.com/a/18602474/235633

这篇关于未知属性 (w)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆