Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
This example uses KarenS' Date module to create a date field, "field_date", with both a start time and an end time that records hours and minutes. We then create a new computed field to calculate the duration as a decimal number of hours (where 1.5 is 1 hour and 30 minutes).
Computed field settings
- Computed Code:
$field_info = array_pop(field_get_items($entity_type, $entity, 'field_date')); $start = $field_info['value']; $end = $field_info['value2']; $timezone = $field_info['timezone']; $date_start = new DateTime($start, $timezone); $date_end = new DateTime($end, $timezone); $interval = $date_start->diff($date_end); $entity_field[0]['value'] = $interval->format('%h');
- Display Code:
$display_output = $entity_field_item['value'] . " hours";
- Check "Store using the database settings below".
- Data Type: float
- Data Length: 3,2
Now if you set the start time field to 0900h and the end time to 1130h, your computed field will store the value "2.5" and display "2.5 hours".
Comments
Interest rate?
Could this (something similar) be applied to money to pay calculating the total debt with a certain interest rate?
It could be between dates calculated in months...
Anyone know how to make this
Anyone know how to make this work in D6?
Drupal 6 code.
Yes I spent a couple of hours getting it working with D6.
I changed the code a bit to make it more flexible.
Here is the computed code.
NOTE: change "$node->field_datefield" to the field name of your date field.
And the display code.
Store in database.
Data type: float
data Length: 5,2
Sortable = checked.
Mix of Datetime and Date
Is it possible for this code to work on two fields where one is a datetime field and one is a date field?
"Only" alter one date
Hello cannod,
hope you can help. I used your code and it is working. Great.
But I'm in need to alter one of the datefield values and lack the basic coder know how. How to I store a date? Even this simple, untouched copy doesn't work:
: $start_value = $node->field_datefield[0]['value'];
: $node_field[0]['value'] = $start_value;
Is it possible to handle datefields? How do handle multiple values. What is the display code for multiple data?
Background: My users found a way to misuse the setup as they cheat by enter a start value in the past just to get on top of the list. I like to correct that by compute the max($start_value, today), so all nodes have the same start date when the event already started.
All in all:
: Read two values from a multiple value datefield in start_date and end_date;
: if (end_date > today)
: : if (start_date < today) start_date = today endif
: endif
: Return a result with two values: start_date & end_date
I'm a little bit ashamed that I can't write the php code for that. Sorry, I'm at the very beginning.
ThanX, Ralf
Duration - Output format varies depending on size of duration
is there a way to vary the out put format
for instance.
in days if duration is <1.5 weeks
in weeks if duration in <2 months
in months id duration is < 2 months
Thanks,
Lauren
Drupal 7 code
Following code is the D7 equivalent of the above D6 code.
How to sum up all the durations and get total durations ?
This code worked for me ,I have multiple durations (multi value field collection item) how to add up and display the sum in another field?
ex:Duration1=10 months
Duration2=12 months
Total duration=22 months
summing up all the values in field collection
calculating the duration worked out using the computed field
I have requirement to
1.sum up all the durations in a work experience field collection in resume,
help me please pl.
2. Calculate the duration from the oldest start date to Date Now()
help please
have look at screen shot
field_get_items()
Looks good, but field_get_items() should be used instead of digging into the field directly.
how to calculate the days between the from and to date?
I want to calculate the days between the from and to date.
the following code does not calculate the days
I have the value start and end date.
when i calculate date the Page displayed blank with ... (Object) DateInterval
Drupal 7 Calculate Days from (or between) Start date & End Date
If the Machine name of your date-field is 'field_example_date', then the Computed Code (PHP) would be
The Display Code (PHP) will have
Notes -
My Requirement was to calculate the no of days a leave-request was applied for based on the start & end dates.
So even if its a one day leave, a start date would have to be entered surely & where an end date would be skipped.
In that case, alter code
using that code everything
using that code everything work
Today I was working on my site and I added a new content where:
where start is 2015-03-27T00:00:00
and the end is 2015-03-31T00:00:00
the result is 2.95833333333
why ?
Thanks
Could not use code
Hi
I am having trouble using the code for computed field. I am using Drupal 6.
I have defined two fields:
field_d1
field_d2
and both of them are date fields using KarenS' Date module.
I have used both the code in this page, and cannod's code. But none seem to be working!
Could anyone please help. Appreciate it.
Thanks
You can just do this if
You can just do this if you're using a single date field with from/to values:
or this if you have 2 separate date fields:
you can specify 'seconds', 'minutes', 'hours' or 'years' in the third parameter for what you want your value displayed as.
calculate duration error
Can anybody help me with this.I want to calculate duration for working hours on certain ticket only.For example the duration start at from 9am until 5pm a day.Then exclude Saturday and Sunday.Currently the duration calculate from the start date until end date including off working hours and that is not so precise.Can anybody gift me guide or anything.
need help for this one
i need to calculate duration between post date and last comment time. i have the code right here..but the code doesn`t working. so i was hoping someone can help me
$start_value = $node->created[0]['value'];
$end_value = $node->last_comment_timestamp[0]['value'];
$timezone = $node->last_comment_timestamp[0]['timezone'];
$type = $node->last_comment_timestamp[0]['date_type'];
$start_date = date_make_date($start_value, $timezone, $type);
$end_date = date_make_date($end_value, $timezone, $type);
$exacthour = date_difference($start_date, $end_date, 'hours');
$node_field[0]['value'] = "$exacthour"
hazreen..
Hi, i want to have the
Hi, i want to have the duration of start and end date in weeks, not for 2 different datefields, just by the "add another date" button
1.date 16 weeks
2.date 4 weeks
and so on.
when i use the code for D7 from above:
and for the output this one
it shows me in both fields the result from the first field i entered (16 weeks).
Am I stupid or just didn`t read enough howtos? How should i do this?
How to get the result in months?
How to get the difference in months and also if i want to get some thing like 3.5 years?
This worked perfect for two
This worked perfect for two date fields in my content type unless you only have one field, then it gives a fatal error. How would I tell it not to compute if the two dates were not there? I tried checking the "Not NULL" box to no avail.
Joao
"when the only tool you have is a hammer, all problems start to look like a nail"
Is there a final solution to
Is there a final solution to calculate the end time given the start time and duration?
That would be great!
I have a start date in a CCK
I have a start date in a CCK Date field and an Integer field for days that I want added to the start date. I want to calculate the end date based on Start Date + Days.
Can anybody help?
Does anyone have a solution
Does anyone have a solution for this for the D7 version?
Glenn
THECA Group
Calculate Days from given date and "today"
A bit of a newbie with PHP...trying to calculate difference in days between today and a given date from date field.
Any help would be appreciated..
John
"when the only tool you have is a hammer, all problems start to look like a nail"
time() or $_SERVER['REQUEST_TIME']
That function and static variable will return Unix timestamps. They are different though, use the appropriate one according to your needs. That should get you your "to date" that you need to be automatic. I'm a newbie too, just found that function and variable today, hope it helped.
Thanks, I will try it!
Thanks, I will try it!
"when the only tool you have is a hammer, all problems start to look like a nail"
Create searchable field from calculated output
I would like to do the following:
If To date - from date =
5 days --> "Less than 1 Week'
10 days --> '1 to 2 Weeks'
30 days --> "One Month"
I also need these fields to be searchable in the form of a drop down list which I believe may limit my options for implementing this.
Any ideas...
Thank You!
Can we update this for D7?
Any chance we can update this so that it will work in D7? So much has changed we need to do a lot more than simply change node to entity...
Don't get it to work
Oké, I had DATE (ISO FORMATE) and that returned nothing.
Now i have selected just date and finely something displays (I also had to add [LANGUAGE_NONE]).
Computed Code (PHP)
Display Code (PHP)
Still I have a problem: It show O hours.
I guess I know why, I don't only store time but also date ('cause things can happen overnight).
I think there is a problem with calculating that?
How do I tweak the code on top to work for let's say:
22:00 hours day 1 to 03:00 hours day two... ?
Thanks for all the help !
difference between two days
hello,
i have two date on webform (drupal 7) , first field:alis_tarihi then second field: iade_tarihi.
i want to calculate difference between two days but hours are not important for me (like: difference is 3 days) .
how can i do it?pls help me.
thx
Did you get it to work?
Hi, I was wondering if you ever got this to work? I'm running into the same problem at the moment and I can't seem to get it to work. Thanks!
How to do with Views PHP
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"
Need this calculations using Views PHP
I had created the views and Put code in output code in Global: PHP.. But could not calculate the information needed.
Kindly help me to solve this problem as needed.
Spreadsheet
Hello sir,
I am using spreadsheet input option for time entries,but when i am fetching details from the views the Time format like 9:45:00 is converted to 0.40625 which is divided by 24,i don't know what is the problem behind this and why the time field is getting divided by 24 while retrieving from DB.Please help me on this....I will be very much grateful....
Thanks in Advance.
How to print the date values of multifieds using the views pdf
Kindly visit the following post for the detailed information
https://www.drupal.org/node/2783083
xxx
xxx
Possible to calculate start-end date from duration?
Is there a way to do the reverse of what is being done here? I mean, is it possible to have a field where I input the duration of the event, as well as the start date+time, and the end date+time is automatically set, using the duration of the event. If there is a way to do this, it would greatly help me with a class management system that I am trying to build using this module.
Thanks.