AnswerBun.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!

Related Questions

How to create and check block revisions?

1  Asked on November 8, 2021 by user3560198

   

Custom module with config exposed in json api

1  Asked on November 8, 2021 by daniel-harper

   

Not able to see < character in front end

2  Asked on November 5, 2021 by jos-l

 

Limit filter operators

1  Asked on November 5, 2021

   

Dynamic filters based on another filter

1  Asked on November 5, 2021 by soundcheese

   

How to add a field to menus?

1  Asked on October 26, 2021

     

Drush cr is responding with Bootstrap Failed

0  Asked on October 26, 2021 by chanel

 

How do I create a table when a module is installed?

1  Asked on October 26, 2021 by david-montera

     

Approaches for multi-field values

0  Asked on October 26, 2021 by ian-stewart

   

Pager not being rebuilt after $form_state->setRebuild() is called

0  Asked on October 26, 2021 by kenny83

 

Where are Commerce 2 hooks (or Events) in Commerce 2

1  Asked on October 26, 2021 by wally-kolcz

     

Show own content in Media Library

0  Asked on October 26, 2021 by rafaticarte

   

Ask a Question

Get help from others!

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