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

Great Thank you !

Assigned:donatoo» Unassigned
Status:Active» Fixed

Status:Fixed» Closed (fixed)

How do we use this? I have the Date API enabled and yet I don't see any options for making calculations for dates.

-R

Status:Closed (fixed)» Active

Interested in this as well. I have two different date fields, and would love to calculate how many days are in between both.

I 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.

Thanks for the detailed how-to, rbrownell!

Issue summary:View changes

Thanks to #8. Also I found this: https://drupal.org/sandbox/busla/1943938