Fatal error: Call to a member function diff() on a non-object I have an entityform that has a computed field.
In computed field,
I have to compute two time fields.

When I tried using this code that declares value for the two time field, it works fine

$timefrom = '06:00PM';
$timeto = '8:30PM';
$from = DateTime::createFromFormat('h:iA', $timefrom );
$to = DateTime::createFromFormat('h:iA', $timeto );

$ot_hour =  $to-&gt;diff($from);
$ot_minute =  $to-&gt;diff($from);
$hour = $ot_hour-&gt;format('%h');
$minute = $ot_minute-&gt;format('%i');
$hourtominute = $hour*60;
$total = ($hourtominute+$minute)/60;
$entity_field[0]['value'] = $total;


But when I used the variables from the entityform, 

$timefrom_value = array_pop(field_get_items($entity_type, $entity, 'field_time_from'));
$timeto_value =array_pop(field_get_items($entity_type, $entity, 'field_time_to'));

$timefrom = $timefrom_value['value'];
$timeto = $timeto_value['value'];
$from1 = DateTime::createFromFormat('h:iA', $timefrom );
$to1 = DateTime::createFromFormat('h:iA', $timeto );


$ot_hour =  $to1-&gt;diff($from1);
$ot_minute =  $to1-&gt;diff($from1);
$hour = $ot_hour-&gt;format('%h');
$minute = $ot_minute-&gt;format('%i');
$hourtominute = $hour*60;
$total = ($hourtominute+$minute)/60;
$entity_field[0]['value'] = $total;


It show error "Fatal error: Call to a member function diff() on a non-object"

Why is this happening?
I need some help to solve this. thanks a lot!
It seems like you're getting confused with your $to and $to1 objects. There is typo in your code. Try this on your later code:

$ot_hour =  $to1-&gt;diff($from);
$ot_minute =  $to1-&gt;diff($from);
$hour = $ot_hour-&gt;format('%h');
$minute = $ot_minute-&gt;format('%i');
$hourtominute = $hour*60;
$total = ($hourtominute+$minute)/60;
$entity_field[0]['value'] = $total;