Using computed field to calculate total hours for a datetime field but to exclude all weekend hours
I would :

$duration = $start_date-&gt;difference($end_date, 'hours');
// Place this :
if($start_date-&gt;format('Y-W') != $end_date-&gt;format('Y-W')) {
  $difweeks = $start_date-&gt;difference($end_date, 'weeks');
  $duration = $duration - intval(48 * $difweeks);
}
$entity_field[0]['value'] = $duration;