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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
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.
Standard Usage
7 8 9 10 11 12 |
|
Count “Nights” Only
End date is not included in the day count so you are essentially counting the number of “nights” between the two dates.
14 15 16 17 18 19 20 |
|
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!
Parameter Details
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.