Collins
Collins

Reputation: 1149

Drupal: Fatal error: Call to a member function format() on a non-object

I have declared the dates as strings, but these will be inputted by the user using a date picker.

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);

}

I get the error "Fatal error: Call to a member function format() on a non-object in C:\wamp\www\booking\sites\all\modules\contrib\availability_calendars\availability_calendar.inc on line 617" which is...

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

I can't go changing this line, as I want to extend this module, not rewrite it, so there must be something wrong with the way I am setting the date for $from and $to in the first bit of code.

Can anyone help?

Upvotes: 1

Views: 409

Answers (2)

Collins
Collins

Reputation: 1149

The problem was that the date was coming through as a unix time stamp and not a date object.

The working code...

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);

}

Upvotes: 1

Aroic
Aroic

Reputation: 479

From what I found on the manual and my intuition, it seems that using double quotes is causing the createfromformat() function to return the output as a string. Using single quotes should return it as an object.

Specifically:

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

Upvotes: 0

Related Questions