TransWikia.com

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

Drupal Answers Asked by saintslash on November 11, 2021

Please please if can anyone help I would really appreciate it!

I have managed to get total hours between value 1 and value 2 of my datetime field however I really want to exclude any hours which are in the weekend. I am creating a timesheet for a group of carers and some people will work from a friday night to a saturday morning etc.

this is my code I use atm for calculating the total hours:

$start_value = $entity->field_work_hour_date[LANGUAGE_NONE][0]['value'];
$end_value = $entity->field_work_hour_date[LANGUAGE_NONE][0]['value2'];
$timezone = $entity->field_work_hour_date[LANGUAGE_NONE][0]['timezone'];

$start_date = new DateObject($start_value, $timezone);
$end_date = new DateObject($end_value, $timezone);

$duration = $start_date->difference($end_date, 'hours');
$entity_field[0]['value'] = $duration;

I am still quite new to drupal and php so I would really appreciate any help.

Cheers

One Answer

I would :

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

Answered by Gregory Kapustin on November 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP