[英]Drupal: Fatal error: Call to a member function format() on a non-object
我已將日期聲明為字符串,但這些將由用戶使用日期選擇器輸入。
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 中的非對象上的成員函數 format()”,這是...
->condition('date', array($from->format(AC_ISODATE), $to->format(AC_ISODATE)), 'BETWEEN')
我不能改變這一行,因為我想擴展這個模塊,而不是重寫它,所以我在代碼的第一位設置$from
和$to
的日期的方式一定有問題。
任何人都可以幫忙嗎?
問題是日期是作為 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);
}
根據我在手冊中的發現和我的直覺,似乎使用雙引號會導致createfromformat()
函數將輸出作為字符串返回。 使用單引號應將其作為對象返回。
具體來說:
$from = DateTime::createFromFormat('Y-m-d h:i:s', $start_date);
$to = DateTime::createFromFormat('Y-m-d h:i:s', $end_date);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.