1

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

0

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;
  • thanks so much for your help but I am still unable to get this to work. I am unsure if i need to define the $duration first like I have done to work out the total hours (with start date->difference). – saintslash Feb 5 '13 at 11:00
  • Why not ? It's either that or a while loop, so better do that, no ? – Gregory Kapustin Feb 5 '13 at 11:03
  • thanks so much for your help. I still can't get this to work though. I am unsure where I need to add this code to my original, i have tried a few combinations but I either get the total hour non dependent on the weekday or 0 hours. any extra help would be amazing and sorry if i am a bit slow working it out! – saintslash Feb 5 '13 at 11:05
  • sorry just seen your comment. what is a while loop? thanks – saintslash Feb 5 '13 at 11:06
  • I edited my answer to fit into your code. – Gregory Kapustin Feb 5 '13 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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