February 10, 2015: Fixed issue where non-US date formats were not working as expected. Added link on demo to alternate version.
September 17, 2014: Fixed issue with newer versions of jQuery UI Datepicker where "change" event was not fired when a date was selected and day count was not calculated.
March 15, 2013: Updated to fix issue when conditional logic was used on the form.
March 3, 2013: Updated to fix issue where "include_end_date" option was not working and added better support for different date formats.
March 2, 2013: Updated to support applying to multiple sets of date fields on the same form. Also updated to fix issue where functionality did not run when form was returned due to validation error.
Have you ever needed to calculate the number of days between two given date fields? Here’s a scenario for consideration.
Let’s say you have a Gravity Form set up to register users for a multi-day event. The user is able to select a start date and end date that they will be attending the event. For each day of attendance they must pay an attendance fee of $10. How can we calculate the total price they should pay for registration?
This snippet will allow to calculate the number of days between two date fields and then populate the calculated number of days into another field. This is beneficial because with this calculated number of days now available as a field value, we can use Gravity Forms’ Calculation Product to correctly calculate the registration fee based on the selected dates.
How do I install this snippet?
Easy peasy. Just copy and paste the code above into your theme's functions.php file.
How do I use this functionality?
To use this snippet’s functionality just go directly below the snippet and instantiate the
GWDayCount() class. Instantiate is a big word but all it means is that you’ll be creating a new “instance” of the
GWDayCount() class with a set of parameters (aka options) for that specific instance.
new GWDayCount( array( 'form_id' => 9, 'start_field_id' => 1, 'end_field_id' => 2, 'count_field_id' => 3 ) );
End date is not included in the day count so you are essentially counting the number of “nights” between the two dates.
new GWDayCount( array( 'form_id' => 9, 'start_field_id' => 1, 'end_field_id' => 2, 'count_field_id' => 3, 'include_end_date' => false ) );
Once you instantiated the class you are finished! If you would like to have this functionality on multiple forms then just create a new instance of the class and fill in the parameters for the new form. That’s it!
form_id: The form ID of the form you would like to apply this functionality to.
start_field_id: The ID of the date field that will contain the start date. This field must be a date field and the parameter only holds a single field.
end_field_id: This parameter holds the ID of the date field that will contain the forms end date. This field, like the start_field_id field, must be a date field and only holds a single field ID.
count_field_id: This parameter holds the ID of the field that will be populated with the calculated number of days between the start date and end date.
include_end_date: Defaults to true. Set to this false if you would like to count only “nights” where the start date would be the check-in date and the end date would be the check-out date.
Points of Note
- This is currently only setup to work with “Date Picker” date fields. Let me know if you need this with other types of date fields in the comments.