Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
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).
Comment | File | Size | Author |
---|---|---|---|
#10 | date-calculate.PNG | 69.36 KB | augustynen |
Comments
Comment #1
KarenS CreditAttribution: KarenS commentedFinding 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.
Comment #3
videographics CreditAttribution: videographics commentedThanks 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:
The Display Code field is:
One can substitute "minutes", "days" or some other value for "hours".
(Also, be sure to leave out the PHP tags.)
Comment #4
kreutzer CreditAttribution: kreutzer commentedThanks 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?
Comment #5
videographics CreditAttribution: videographics commentedkreutzer, 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:
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:
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?
Comment #6
kreutzer CreditAttribution: kreutzer commentedThanks 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.
Comment #7
videographics CreditAttribution: videographics commentedCounting 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.
Comment #8
busla CreditAttribution: busla commentedWould it be complicated to create a new rule action from this code?
Comment #9
augustynen CreditAttribution: augustynen commentedI 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) :
and this is my Display Code (PHP)
and my output is 0 hours
Comment #10
augustynen CreditAttribution: augustynen commentedNow 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.
Comment #11
busla CreditAttribution: busla commented@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
Comment #12
doors CreditAttribution: doors commentedI 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?
Comment #12.0
doors CreditAttribution: doors commentedadded need for hours in output
Comment #13
vmevada102 CreditAttribution: vmevada102 commentedI 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"
But not get a result..
Kindly help me to solve my problem.
Comment #14
fehin CreditAttribution: fehin commented@vmevada102, did you find the solution for views php? I'm trying to do the same thing.
Comment #15
vmevada102 CreditAttribution: vmevada102 commentedI had tried the solution in views php. but could not resolved the same