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 46 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 45+ essential add-ons for Gravity Forms with support you can count on.
Done installing the snippet. How to use this? Thank you in advance :)
Got already on how to use the snippet. But how can I use it also on my other form?
This will help: https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/
Hi, this works great. Do you have any suggestions on changing the date for datepicker 2 if datepicker one becomes the same or greater than datepicker2?
Gravity forms article section: 4. Datepicker 1 + 1 month becomes minDate for datepicker 2 suggests how to make date picker 1 the minimum for datepicker 2 but this breaks the counter if say you set the two date pickers and then move datepicker 1 backwards because datepicker 2 will change. Here’s the article for reference.
https://docs.gravityforms.com/gform_datepicker_options_pre_init/#3-datepicker-1-becomes-mindate-for-datepicker-2
Disregard, I was able to solve this.
Hi,
we are getting the following error when trying to install the snippet in the child theme’s function.php:
Uncaught Error: Class ‘GWDayCount’ not found in wp-content/themes/oceanwp-child-theme-master/functions.php:32 Stack trace:
0 wp-settings.php(499): include()
1 wp-config.php(90): require_once(‘/homepages/26/d…’)
2 wp-load.php(37): require_once(‘/homepages/26/d…’)
3 wp-admin/admin.php(34): require_once(‘/homepages/26/d…’)
4 wp-admin/theme-editor.php(10): require_once(‘/homepages/26/d…’)
5 {main}
thrown
Any idea how to troubleshoot this one?
Hi Christoph, give these troubleshooting steps a try: https://gravitywiz.com/documentation/snippet-troubleshooting/ My instinct is that you’ve installed the configuration part of the snippet but not the full snippet. You’ll need both.
Sorry, you were right. My bad! Thanks for your help.
Glad to help. ?
Hi
I did this, all looking fine for me. My next step is, that I have prices for a service, and they vary depend of the amount of days.
Any idea, how is the best way to calculate and get a number, where the amount per days is vary?
Regards,
Simon
Hi Simon, it really depends on your pricing logic. If it’s just adjusting the cost by a static amount as the number of days increases, our Gravity Forms Conditional Pricing perk might be a good fit.
Hi, how do i add to two separate forms? I attempted to just add another line of this text :
“new GWDayCount( array( ‘form_id’ => 5, ‘start_field_id’ => 4, ‘end_field_id’ => 8, ‘count_field_id’ => 31, ‘include_end_date’ => false ) );”
Changing the form id, but it isn’t working on the new form. Am I doing something wrong?
Hi this now gives a pluggable error when implemented in functions.php file. Any chance you have a fix for this?
This is typically an issue installing the snippet. Try these steps: https://gravitywiz.com/documentation/snippet-troubleshooting/
Actually, it was being caused because of the tags within the code. I replaced it with an enqueue script function and called a separate file with the script.
Hi Esther, this might also be helpful: https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/
What type of field do I need to use to make this work? I tried with a number field but it is not populating.
FYI, I purchased the 1 perk package (conditional dates) if that has anything to do with it.
Actually, sorry. It is working with the number field. I forgot to clear my cache. Sorry!
Glad you were able to get this working. ?
I want to know if this works with what im trying to do is.
when a customer select a start date and end date it checks the month if its from january to january then it charges $80 if the date is between january to february then is should charge 80+70 and so on.
would I be able to do something like this with this ?
Does it actually matter which months or are you just attempting to calculate the number of months and then the first month is $80, second month is $70, third month is $60, and so on?
Hi, I’m stuck at this step
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!
What does it mean to instantiate the class?
Hi Ivonne, 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. See the “Standard Usage” usage example.Hi,
The snippet works great. But the jQuery script (i.e. window.gwdc) gets added at top of the HTML before DOCTYPE when the gravity form is loaded in a popup using any plugin which throws jQuery not defined error. Do you have any solution for that?
Thanks, Mou
Hi Mou, we have made some improvements to how we load scripts in snippets but they have not been implemented here. If you have any PHP experience, here’s an example of how the scripts should be enqueued: http://snippi.com/s/1dj9jke This snippet is in the queue for an update. No definite ETA.
Is it possible to calculate the range between two dates and recalculate the subtotal at checkout page? Yes, we are trying to use it as a simple booking system.
This snippet would allow you to calculate the number of days between two dates. You could use that in a Product calculation field to calculate a product price based on that number.
Fatal error: Call to undefined function wp_parse_args() in E:\xampp\htdocs\Funeral-Homes-Portal\celi.php on line 20
Hi ging, it sounds like you installed the snippet in the wrong file. Try this: https://gravitywiz.com/documentation/snippet-troubleshooting/
Is there anyway to calculate the range between two dates excluding a certain day such as sundays??
Hi Ruairi, Date Time Calculator supports calculating weekdays and weekendDays between two dates but does not support calculating the number of a specific day.
Hi, is there a documentation on how to use the GP Date Time Calculator?
Hi Shane, if you’re a Gravity Perks user we can a) send you an early-access copy of Date Time Calculator and b) provide a video overview of how it works. Drop us a line via support.
Hi, How do I use this? Not sure I understand the below. Is there an example?
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.
Not understanding the above.
This might help: https://gravitywiz.com/documentation/snippet-troubleshooting/
Dear David
Thanks for the code!
” Uncaught Error: Class ‘GWDayCount’ not found in wp-content/themes/oceanwp-child/functions.php:25 “
Best Tal
Hi Tal, we do have a plugin version of this functionality called Date Time Calculator that is available by request to Gravity Perks users. If you’d prefer to use this snippet, you might find these troubleshooting tips helpful: https://gravitywiz.com/documentation/snippet-troubleshooting/
Thanks David Your link helped me see where I had a mistake. But, I now have another problem implementing it… Is it possible to pay for this days ability only? (we really don’t need the rest of the really awesome ‘perks pack’, and also trying to keep the website lean as possible). Tal
Hi Tal, it sounds like you need the Basic package which gives you access to one perk on one site. You never have to worry about leanness with Gravity Perks because you only install the perks you need.
Well… Just bought it :-) Going to look for the instructions. Thanks Tal
Awesome! Drop us a line via support and we’ll get you the latest version of Date Time Calculator. ?
David, I’d like to use this snippet to calculate the difference between two dates: – today – start of an event date I will not use date picker fields as the second date is set and will (in a later form update) be populated using your populate anything perk as it is taken from a clan calendar plugin. Do you have a version of this snippet that works with regular date fields in this manner?
Ignore the above request. It seems that using the date picker format makes it easier to populate the field dynamically as one input is provided (as opposed different year, month and day fields when the date type is selected). Only weird thing is that apparently a separate filter setting is required to make a datepicker actually read-only when using the relevant perk.
Yes, readonly only applies to the manual user input (just like in your browser) by default. Javascript is able to interact with inputs regardless of the readonly attribute so an additional filter is required if you want to disable the datepicker.
David, okay thanks for the explanation. I’ll drop the relevant code in my snippets plugin items (nice alternative for functions.php which gets overwritten on theme update or having to struggle with child themes tec.)
David,
What I want to do is a little complex. Or at least it is for me. I am, sadly, not a coder.
I want to notify our clients as to when their stuff will be ready. I want to take today’s date and add 21 days to it.
I want to have something at the bottom of the form, before they submit, that says “Your documents will be created on or before 3_weeks_From_Today”
Ideally, I would have a single radio button or checkbox that says “I understand my documents will be ready on variable_date.”
But I realize that’s a probably a tall order!
Any feedback is greatly appreciated, even if its: “Hire a coder!”
Thank you in advance.
Hi Michael, you could use our Populate Dates snippet to populate the 3-weeks-from-today Date into a field and then use our new Populate Anything perk to pull that date into a Checkbox label or HTML Field content.
For my project, I needed to modify the code slightly because the javascript being output in this function “function load_form_script( $form )” was preventing my redirect at the end of my form.
(I’m using Gravity Forms to generate a custom product and add it to the cart – without ajax – and then redirecting to the cart upon completion of the form).
The solution was to remove the entire chunk of javascript (line 50 to 142) and put it inside the form in a HTML block.
I also cleaned up the PHP by removing the “self::$script_output = true;” both above and below where the javascript was, and not more “can’t modify headers” error after submitting my form.
Hope this helps others who run into this issue.
When I add the code into my functions file, WordPress get the white screen of dead. Are there some changes needed for WP5?
Hi Robbin, this might help: https://gravitywiz.com/documentation/snippet-troubleshooting/