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.
Stop! There's a better way.
This snippet is available as a plugin with Gravity Perks, a suite of over 47 premium Gravity Forms plugins!
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.
Standard Usage
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!
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.
Did this resource help you do something awesome with Gravity Forms?
Then you'll absolutely love Gravity Perks; a suite of 47+ essential add-ons for Gravity Forms with support you can count on.
Hi got error while adding code in function.php.
Your PHP code changes were rolled back due to an error on line 191 of file wp-content/themes/astra/functions.php. Please fix and try saving again.
syntax error, unexpected token “<“, expecting end of file
Hi Amit,
The error message is because of how you added the code to your website. Check this documentation on how to install a snippet.
Best,
Hi, Great snippet and works great. Am just having an issue whereby the calculated days are not showing correctly in the notification emails sent. On screen all is 100% but when I receive the notification, the days are always set to 1. Any ideas?
Hi Ivo,
I’m not sure why that is happening. We’ll need to dig into this further to know what’s happening. I’ll contact you via email to request additional information.
Best,
Ok, thanks. Look forward to your email.
Hi Samuel, You manage to look into the issue or perhaps you can send me an email so I can you more information?
Hi Ivo,
I sent you an email on 28th June 2023, requesting you to send us an export of the form. I guess you didn’t receive it. Since you have an active Gravity Perks license, can you submit a support ticket with us via our support form and attach an export of the form.
Best,
Hi Samuel,
Found your email on the server and have replied.
Hi David, a great snippet of code. Thank you.
I would like to know if it is possible, to calculate only the first 7 days of the reservation and not to calculate the rest days as an additional cost. Because we want to charge only for the first 7 days and the rest to be free, Thank you.
Hey Mat, glad you’re enjoying the snippet! Unfortunately, it doesn’t support setting a “max” value.
This would be possible using Date Time Calculator to handle the date calculation and Advanced Calculations
min
method which would return the lowest number (e.g. 7 or the actual number of days calculated).First, sorry for my english.
I’m trying this code, but it doesn’t work. I’ve added the code to functions.php (I have also tried with the snippets plugin) and filled in the form data in the function, but not show anything on view.
Fecha inicio (date field) ID: 1 Fecha fin (date field) ID: 2 Diferencia de dias (number field) ID: 3 Form: ID 5
Configuration
new GWDayCount( array( ‘form_id’ => 5, ‘start_field_id’ => 1, ‘end_field_id’ => 2, ‘count_field_id’ => 3 ) );
But not show anymore:
http://clinicacamposfisioterapia.com/view/diferencia-fechas/
Can you help me, please?
Hi Alexis,
The snippet works with Gravity Forms and not GravityView. If you want a solution that works in GravityView, then you can take try our GP Date Time Calculator Perk.
Best,
hi,
I used this code to check address history if it is up to 5 years and when I created the form it worked, but if add the form to the Nested form the calculation stops working. How do I use it with a nested form to calculate and validate address history?
Hi Ezex,
We’ll need to take a closer look at your setup to determine what’s wrong. If you have an active Gravity Perks License, you can get in touch with us via our support form with additional information. You may also want to try using our GP Date Time Calculator Perk for this functionality.
Best
Hi sir, Im all new to all wordpress and php stuffs. i need your help.
i tried to use this and it doesn’t work at all for me. I copied the 2000 line code without any error following your guide. then i copied your new GWDayCount for the night count
new GWDayCount( array( ‘form_id’ => 60, ‘start_field_id’ => 14, ‘end_field_id’ => 13, ‘count_field_id’ => 62, ‘include_end_date’ => false ) );
i updated the functions.php and no error so far. i even rebooted the whole wordpress and server to be sure everything would work. This is the step i’ve done.
Hi Frederic,
If the setup is correct it should work as expected. You can try using Code Snippets plugin instead of modifying the functions file.
If you have a Gravity Forms License, you can get in touch with us via our support form with an export of your form and any additional information so we can assist you further.
Best,
Hi, when putting this code into FUNCTIONS.PHP I get the following error:
Your PHP code changes were rolled back due to an error on line 34 of file wp-content/themes/uncode/functions.php. Please fix and try saving again. Uncaught Error: Class ‘GWDayCount’ not found in wp-content/themes/uncode/functions.php:34 Stack trace:
0 wp-settings.php(546): include()
1 wp-config.php(98): require_once(‘/home/sq49dtyo/…’)
2 wp-load.php(50): require_once(‘/home/sq49dtyo/…’)
3 wp-admin/admin.php(34): require_once(‘/home/sq49dtyo/…’)
4 wp-admin/theme-editor.php(10): require_once(‘/home/sq49dtyo/…’)
5 {main}
thrown
Hi Ad,
Based on the error message, it appears you’ve only added the configuration of the snippet and not the actual Class. Here is a link to the complete snippet.
Best,
Is there any way to add 14 days to a specific date, but then return the new calculated date in a specific format? For example, date in one field is 6/1/2021 and I want to add 14 days but then have it return “June 15, 2021”
Hi Mikhail,
We have a snippet you can use to modify and populate a Date field with the modified date. Here is the documentation to the snippet with details on the various options available. I would guess a configuration like this would work for you.
Best,
It seems to be working in functions.php file. However when I place it with my other snippets in a separate plugin, it brakes the website. Can there be something done?
Hi Vitaly,
Since it works when placed in the function.php file, I don’t think there is an issue with the code. It could be with the way you’re inserting the snippet in the snippet plugin. If you have a Gravity Perks license, you can get in touch with us via our support form so we can assist you further. That said, here is documentation on how to install a snippet, to see if it helps.
Best,
Thank you for your time Samuel. Yes, the snippet works but, as I said, it breaks the website.
Cheers,
Hi Vitaly,
I’m unable to recreate this issue when I test it with the Code Snippets plugin.
We recommend purchasing the Perk version of this snippet. Not only does it offer more features and ease of use, it also includes support.
how to calculate the days except the weekends? is it possible? thanks
Hi Ruairi, Our Date Time Calculator supports calculating weekdays and weekendDays.
Hi David, I’ve installed the ‘Calculate Number of Days Between Two Gravity Form Date Fields’ snippet and it was working wonderfully in Gravity Forms. However, we’re using Gravity Views and it stops the ‘Edit Entry’ screen from rendering and I don’t understand why. Have you run into this issue with your ‘GravityWiz’ plugin? All I’m doing is finding the days difference between the current date and the user’s data of birth. I’m using your ‘GW_Populate_Date’ snippet prior to the aforementioned snippet to populate the Current date and this appears to work correctly.
Hi Alex, GravityView alters some of the core functionality of Gravity Forms when it renders forms in the Edit Screen. Unfortunately, we can only guarantee that our snippets will work with Gravity Forms proper. We have so many!
Fortunately, this snippet has a plugin version that will work with GravityView. It’s called Date Time Calculator and it’s even easier to use than the snippet. If you give a shot, I’d love any feedback you have. 🙂
Does this also work with a date-Dropdown instead of a datepicker ?
The snippet version does not; however, the perk version, Gravity Forms Date Time Calculator, does!
Hello, how can i only return results greater than 5, i.e i only want to calculate days and return value greater than 5.
i.e Start date 1st end date 7th returned value = 2 i.e Start Date 1st end date 5th returned value = 0 i.e Start Date 1st end date 3rd returned value =0
i think i need to do a if statement, return value if greater than 5-5?
Hm, you could have two calculation fields with the same formula and then use conditional logic to show/hide the second calculation field if the value was greater (or less than) 5. Then you would only use the second calculation field’s value.
what if the opposite. I have a member data that entered the x date with a validity period for y months. What date will the membership end. What plugins or GW solutions are suitable for the solution?
Hi Laras, it sounds like you’re looking for: https://gravitywiz.com/populate-dates-gravity-form-fields/
I’m not a programmer, but I manage to insert the code to my child theme functions.php, and had modify the Form ID and Field ID as accordingly
But after that I do not know what to do.
On My Gravity Form itself I had 1. Start Date (Date Field) 2. End Date (Date Field 3. Duration (Text Field)
at the Duration Field I checked “allow field to be populated dynamically” then enter the parameter field as “GWDayCount”
Am I doing it correctly?
Hi ET, it sounds like you have field types right but this doesn’t use dynamic population. If you’ve configured the snippet correctly, it should “just work” after you’ve replaced the field IDs with your own. More help here: https://gravitywiz.com/documentation/snippet-troubleshooting/
thanks! works great – but I have the same question as Robert: I want to calculate years and not days. How do I do that? Basically I’ve set the “start_field_id” to a “birth date” field, and the “end_field_id” to a date field with default value of “{date_mdy}” – so I get the calculation between the birthday and today – but i get it in days, whereas i want it in years. is this done via the Gravity Forms Date Time Calculator perk? and if so – how and where do i get this perk? it’s not on the perks list. thx!
You could always divide that number by 365. Results may not be 100% accurate but they’ll be pretty close. The Gravity Forms Date Time Calculator perk handles this a little more robustly. This early access perk is available by request to any Gravity Perks customer. ?
Hi David, thanks for this info. Could I use this code to calculate age (today minus date of birth)?
Thanks.
Sure. Or if you’re a Gravity Perks customer, just request the Gravity Forms Date Time Calculator perk which includes an :age modifier for calculating this with a single date field.
So this is a bit of an odd one… it works perfectly until I enable Partial Entries. Once I do that the window.gwdc script is moved to before the opening html tag.
Hi Peter, this is unlikely something we will fix in the snippet-version of this; however, we do have an early-access perk called GP Date Time Calculator that offers a far superior solution. It’s available to any Gravity Perks customer by request.