I'm trying to use the computed field module to calculate the difference between the start and end of a date entry and I'm having a terrible time. I found an old snippet for older versions in the docs (http://drupal.org/node/149234) but I'm not able to update it successfully. I ultimately need to store and output a decimal value of the duration (number of hours).

CommentFileSizeAuthor
#10 date-calculate.PNG69.36 KBaugustynen
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

KarenS’s picture

Status: Active » Fixed

Finding the underlying values in the D7 field system is a little tricky. You need to use something like field_get_items('node', $node, 'field_name'); That will give you an array containing 'value' and 'value2'. Then you can do your computation on that.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

videographics’s picture

Issue tags: +computed field

Thanks Karen. I did actually get this working.

In case anyone needs this, here's an example of what to put in the Computed Code field:

$start_value = $entity->field_timewindow[LANGUAGE_NONE][0]['value'];
$end_value = $entity->field_timewindow[LANGUAGE_NONE][0]['value2'];
$timezone = $entity->field_timewindow[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;

The Display Code field is:

$display_output = $entity_field_item['value']." hours";

One can substitute "minutes", "days" or some other value for "hours".

(Also, be sure to leave out the PHP tags.)

kreutzer’s picture

Thanks videographics. This helped me a lot. But this function works well only for defined start and end dates. What about users who don't set an end date, but instead use the Date repeat function to indicate the number of days/occasions? What kind of code would cover all the different repeat rules?

videographics’s picture

kreutzer, I'm not sure I understand your question. Are you trying to calculate the difference between two repeating dates – something like "How long until the next repeat instance?' If what you want to do is count the number of repeats, you would do that in another way entirely.

I can tell you that the example code in #3 doesn't care where the date/time values come from. You could easily replace the line:

$end_value = $entity->field_timewindow[LANGUAGE_NONE][0]['value2'];

with a reference to a field other than timewindow and reference the first value. For example you could replace the line above with something like:

$end_value = $entity->field_otherdatetimevalue[LANGUAGE_NONE][0]['value'];

I've done this in certain situations and it works fine. (no surprise)

That said, so far I haven't had much of a need to reference repeating dates in D7 and I don't know how well it's documented. Can someone else provide some guidance here?

kreutzer’s picture

Thanks videographics. Let me rephrase. I need to calculate (count) the number of repeats for a specific event. Let's say for example that I wish to attend a workshop and that I can opt to attend this workshop every day, once a week, or once a month for the next few months. So I decide that I would like to attend the workshop once a month, for the next six months, starting from Jan 15 (start date). In the "Date Repeat" function, after selecting the StartDate, I make it repeating once every month for 6 months". Or another way to choose that option is to select "1 times - every month, ending on a specific date" which also automatically calculates the number of months. In my CCK computed field, I need to pick up the number of workshops that I will be attending, which in the example above is six (6).

Am not very clear about what you suggested. Could you paste the complete code that would go in the CCK field? This would help me more clearly understand the different variables that are involved.

Thanks again.

videographics’s picture

Counting repeat instances is going to be very different from computing the duration between two date/time values.

Kreutzer, what you seek is simply outside the scope of this issue so you should probably be creating a new one.

busla’s picture

Would it be complicated to create a new rule action from this code?

augustynen’s picture

I don't get it, with snippit I use, the result is the same: 0 hours.
Nothing gets calculated.

Can someone give an example of D7 with the date calculation en computed field?
But really step by step?

I have a field_date with a start AND end date + time (11/03/2013 - 14:00 to 14:30)
and a computed field field_time

this is my Computed Code (PHP) :

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

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

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

and this is my Display Code (PHP)

$display_output = $entity_field_item['value']." hours";

and my output is 0 hours

augustynen’s picture

FileSize
69.36 KB

Now it gets even more interesting:
I've taken a look at the datebase and changed the field_timewindow to field_data_field_date
and the value's to: field_date_value, field_date_value2

Result:

( ! ) SCREAM: Error suppression ignored for
( ! ) Fatal error: Call to a member function getName() on a non-object in C:\wamp\www\drupal\sites\all\modules\date\date_api\date_api.module on line 286

I've attached a screenshot of the error.

busla’s picture

@augustynen: I had the same issue and solved it creating a simple action module for Rules that accepts two parameters,earlier and later dates and calculates the time difference using the $startdate->difference($enddate, 'years'); method.

It depends on the Date and Rules (obviously).

http://drupal.org/sandbox/busla/1943938

doors’s picture

I am using a date field in a training log content type with start and end date mandatory. Hours and minutes are used and I am finding it difficult to get a durtion for each training and then sum up at those durations for a summary of work out.

I have tried this and it works but I would want to show 7:20 instead of 7.33333 and still allow for views sum aggregation to work properly. The timefield module does this perfectly but the timefield doesn't work with the Calendar module and I also need the day and year fields.

Can anyone help me?

doors’s picture

Issue summary: View changes

added need for hours in output

vmevada102’s picture

Issue summary: View changes

I had used the above said example. but now my content type has the data and not able to delete or add any field in it.

I had created the experience field in content type in the Multifield table module.

Now would like to calculate the difference using Views PHP module.

But could not calculate using the details

COdes in "Output Code"

<?php

$start_value = $row->field_experience_faculty_field_duration_exp_value;
$end_value = $row->field_experience_faculty_field_duration_exp_value2;

$start_date = new DateObject($start_value);
$end_date = new DateObject($end_value);
$duration = $start_date->diff($end_date);


echo $duration;

?>

But not get a result..

Kindly help me to solve my problem.

fehin’s picture

Issue tags: +views php

@vmevada102, did you find the solution for views php? I'm trying to do the same thing.

vmevada102’s picture

I had tried the solution in views php. but could not resolved the same