0

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->diff($from);
$ot_minute =  $to->diff($from);
$hour = $ot_hour->format('%h');
$minute = $ot_minute->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->diff($from1);
$ot_minute =  $to1->diff($from1);
$hour = $ot_hour->format('%h');
$minute = $ot_minute->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!

closed as off-topic by kiamlaluno Jun 5 '14 at 4:48

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced, was solved by a cache clear, or was a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers." – kiamlaluno
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Please don't edit your question in a way that invalidates the given answers. If you remove that code, the answer doesn't make sense anymore. – kiamlaluno Jun 5 '14 at 4:46
0

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->diff($from);
$ot_minute =  $to1->diff($from);
$hour = $ot_hour->format('%h');
$minute = $ot_minute->format('%i');
$hourtominute = $hour*60;
$total = ($hourtominute+$minute)/60;
$entity_field[0]['value'] = $total;

Not the answer you're looking for? Browse other questions tagged or ask your own question.