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.
Hi,
I'm trying without success to figure out to calculate the number of days between 2 dates in rules.
Do someone have a solution ?
Thanks in advance for your reply.
Marc
Comments
Comment #1
arlinsandbulte CreditAttribution: arlinsandbulte commentedhttp://drupalcontrib.org/api/drupal/contributions!date!date_api!date_api.module/function/DateObject%3A%3Adifference/7
Comment #2
donatoo-1 CreditAttribution: donatoo-1 commentedGreat Thank you !
Comment #3
arlinsandbulte CreditAttribution: arlinsandbulte commentedComment #4
donatoo-1 CreditAttribution: donatoo-1 commentedComment #5
rbrownellHow do we use this? I have the Date API enabled and yet I don't see any options for making calculations for dates.
-R
Comment #6
rbrownellComment #7
Exploratus CreditAttribution: Exploratus commentedInterested in this as well. I have two different date fields, and would love to calculate how many days are in between both.
Comment #8
rbrownellI ended up using a method that involved converting the dates into a Unix Timestamp (which is based on the number of seconds that have passed since January 1, 1970).
It is interesting to note that there is a Condition that can compare two dates but in terms of generating an integer with the number of days, hours, minutes, or seconds between two dates, that action simply does not exist.
Here is a copy and paste of the Actions I used with a little explanation...
(Convert your first date field to Unix Time)
Convert data type
Parameter: Target type: Integer, Value to convert: [field_date], Rounding behavior: Always up (9.5 -> 10)
Provides variables: Date 1 (date_1)
(Convert your second date field to Unix Time)
(In this case I am using the site's current date but you can include a custom field)
Convert data type
Parameter: Target type: Integer, Value to convert: [site:current-date], Rounding behavior: Always up (9.5 -> 10)
Provides variables: Date 2 (date_2)
(Subtract your two timestamps from one another to get the number of seconds between each)
Calculate a value
Parameter: Input value 1: [date-1], Operator: ( - ), Input value 2: [date-2]
Provides variables: Seconds Between (seconds_between)
(Divide your calculated timestamp by the number of seconds in a day to get the number of days)
Calculate a value
Parameter: Input value 1: [seconds-between], Operator: ( / ), Input value 2: 86400
Provides variables: Raw Days Between (raw_days_between)
(Convert the value to an integer and round to get a whole number (if needed))
Convert data type
Parameter: Target type: Integer, Value to convert: [raw-days-between], Rounding behavior: Always down (9.5 -> 9)
Provides variables: Days Between (days_between)
(Set the value defined by your component (if needed/using a component))
Set a data value
Parameter: Data: [days-until-event], Value: [days-between]
Hopefully this can be of some assistance to you. If you are new to rules (like I was when I came up with this method, it took me a couple of days to figure this out...) and you want to include this value as part of another rule as an action or condition you must use this to make a component that contains this calculation and then include it in another rule.
Comment #9
jiakomo CreditAttribution: jiakomo commentedThanks for the detailed how-to, rbrownell!
Comment #10
lunk rat CreditAttribution: lunk rat commentedThanks to #8. Also I found this: https://drupal.org/sandbox/busla/1943938