Drupal:致命错误:在非对象

时间:2016-06-13 18:39:23

标签: php datetime

我是PHP的新手,我坚持以下......

目前我已将日期声明为字符串,但这些将由用户使用日期选择器输入。

function availability_status($updated_node, $start_date, $end_date, $status) {

  // get the fields we need.
    $cid = $updated_node; 
    $sid = $status;
    $from = DateTime::createFromFormat("Y-m-d h:i:s", $start_date);
    $to = DateTime::createFromFormat("Y-m-d h:i:s", $end_date);

    // change the state to the one selected;
    availability_calendar_update_availability($cid, $sid, $from, $to);

}

我在第617行的C:\ wamp \ www \ booking \ sites \ all \ modules \ contrib \ availability_calendars \ availability_calendar.inc中的非对象上收到错误“致命错误:调用成员函数格式()” “这是......

->condition('date', array($from->format(AC_ISODATE), $to->format(AC_ISODATE)), 'BETWEEN')

我不能改变这一行,因为我想扩展这个模块,而不是重写它,所以我设置$from$to的日期的方式肯定有问题在第一段代码中。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

问题是日期是作为unix时间戳记而不是日期对象。

工作代码......

function availability_calendars_rules_rules_action_set_availability_status($updated_calendar, $start_date, $end_date, $status) {

  // get the fields we need.
    $cid = $updated_calendar; 
    $sid = $status;
    $from = date('Y-m-d H:i:s', $start_date);
    $from = DateTime::createFromFormat("Y-m-d H:i:s", $from);
    $to = date('Y-m-d H:i:s', $end_date);
    $to = DateTime::createFromFormat("Y-m-d H:i:s", $to);

    // change the state to the one selected;
    availability_calendar_update_availability($cid, $sid, $from, $to);

    drupal_set_message('Status has been set to '. $sid);

}

答案 1 :(得分:0)

从我在manual和我的直觉上发现,似乎使用双引号导致createfromformat()函数将输出作为字符串返回。使用单引号应将其作为对象返回。

具体做法是:

 $from = DateTime::createFromFormat('Y-m-d h:i:s', $start_date);
 $to = DateTime::createFromFormat('Y-m-d h:i:s', $end_date);
相关问题