2

I have a date time field to input working hours and then I need calculate total work hours. I was try using Global math expression in views, but the results is 0, although I was try to change views formmater from date and time, ago and plain.

This is the condition, lets say the rule in office is:

Employee In 1: 08.00
Employee Out 1: 12.00
Employee In 2 : 13.00
Employee Out 2 : 17.00

It's mean that employees must have 8 hours a day.

The question is, how can I calculate that employee is late in 2 hours if the field value like this:

filed Employee In 1: 09.00 <- late 
field Employee Out 1: 12.00 
field Employee In 2 : 13.00 
field Employee Out 2 : 16.00 <- earlier out

Is there any module date related or some one chould assist me ho to solve my problem?

  • From 7 am to noon plus 1 pm to 5 pm is 5+4=9 hours, which is MORE then 8 hours. It seems that something in your question (related to "must have 8 hours) is not correctly phrased. If so please EDIT your question to correct that. Also, could it be that you want to validate something like "Start no later then 7 am", and "Finish not before 5 pm"? – Pierre.Vriens Nov 29 '15 at 18:37
  • Extra question: what kind of result would you want if your "rule in office" is not respected. E.g. if 16.00 in your sample would be 18.00 instead? In that case "must have 8 hours" seems satisfied, but not the "rule in office"? Maybe your "rule in office" example needs to be expanded with something like "only the hours between '08.00 and 12.00', and '13.00 and 17.00' should be taken into consideration"? If that is the real validation to be done, then I will update my answer to suggest a completely different solution ... which does not require any PHP coding ... – Pierre.Vriens Nov 30 '15 at 8:37
  • Hi Pierre.Vriens thank for your reply The result i want is to calculate the employee working hours, it should be 8 hours a day and 40 hours a week, and so on. If employee working hours less than 8 hours a day, let say only 6 hours i want to show in cell table (views) that his working hours -2 hours and it will accumulate for a month. So i have a report about an employee working hours for a day and a month. Hope this clarify my goal. And many thanks for your kind to assist me to get solution which does not require any PHP coding. BR, – tinohuda Nov 30 '15 at 14:14
2

Have a look at the Computed Field module. Some details about it (from its project page):

Computed Field is a very powerful field module that lets you add a custom "computed fields" to your content types. These computed fields are populated with values that you define via PHP code. You may draw on anything available to Drupal, including other fields, the current user, database tables, you name it. (Feeling the power yet? :) ) You can also choose whether to store your computed field values in the database with other content fields, or have them "calculated" on the fly during node views. (Although you should note that Views use requires database stored values.) This field is literally the Swiss Army knife of fields. So start cooking up your PHP based values!

You may also want to look at its Community documentation, especially its child page about "Calculating a duration given a start and end time", here is a quote from it:

... create a new computed field to calculate the duration as a decimal number of hours (where 1.5 is 1 hour and 30 minutes).

  • Hi @Pierre.Vriens I try to use Computed Field in Computed Code (php) $in1 = $entity->field_employee_in_1['und'][0]['value']; $ou1 = $entity->field_employee_out_1['und'][0]['value']; $in2 = $entity->field_employee_in_2['und'][0]['value']; $out2 = $entity->field_employee_out_2['und'][0]['value']; $morning = $out1 - $in1; $noon = $out2 - $in2; $total = $morning + $noon; $entity_field[0]['value'] = "$total"; in Display Code (PHP) $display_output = $entity_field_item['value']; But the results is "0" – tinohuda Nov 30 '15 at 1:05
  • Not sure if it is your ONLY problem, but there is at least a typo in your $ou1 (it should be $out1 I think). If that doesn't help, then please EDIT your question to add your prior comment to the end of your question (with something like "based on the suggestion to use computed field ...", if that's what you prefer). – Pierre.Vriens Nov 30 '15 at 8:31
2

You can override views template file and add below code. The below code will find difference between two in-timings and two out timings and calculates total worked hours. This is assuming there won't be exactly 2 in and 2 out.

$start = strtotime($row->field_field_time_in[0]['raw']['value']);
$end = strtotime($row->field_field_time_out[0]['raw']['value']);
$diff = $end - $start;
$secondstart = strtotime($row->field_field_second_time_in[0]['raw']['value']);
$secondend = strtotime($row->field_field_second_time_out[0]['raw']['value']);
$seconddiff = $secondend - $secondstart;
$diff = $diff+$seconddiff;
$hours = floor($diff /3600);     
$minutes1 = intval(($diff/60) % 60);
print $hours.'.'.$minutes1;

You can add any required calculations you require based on this.

  • Hi @AddWeb Solution Pvt Ltd I try your code and modify the field, the result is 0.0 – tinohuda Dec 1 '15 at 2:55
  • In your views tpl, make sure that date fields has proper values i.e $row->field_field_time_in[0]['raw']['value'] should have 2015-11-30 02:45:00. It should be YYYY-MM-DD HH:MM:SS. – AddWeb Solution Pvt Ltd Dec 1 '15 at 7:20
  • Hi @AddWeb I apreciate your help. I was add date custom format Y-m-d H:i:s in admin/config/regional/date-time/formats and use it in configure field formatter in views and set as plain formatter, but nothing printed in views preview. i was do clear cache after saving my views. Also i try to add custom Y-m-d H:i:s formatter in views.tpl and the result still 0.0 – tinohuda Dec 1 '15 at 13:10
  • Can you check, does that field has value? You can see this by showing this field in view itself. It will be helpful if you can show us what output your view provides. – AddWeb Solution Pvt Ltd Dec 2 '15 at 13:01
  • Yes, it has a value: 2015-12-02 08:01:00 others field has the same date formatter. – tinohuda Dec 4 '15 at 1:29

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.