forminator, mu-plugins – difference between two dates
-
I want to be able to calculate the number of days between two dates (from date pickers), then display the result in a field which is used in a calculation field further down the form. I saw a previous post on here with a link to “days-from-date.php”, and instructions on how to insert the code into the mu-plugin directory. I have followed this – ie. created an mu-plugin directory, edited the file for my form id and field ids, and ftp’d the php file into the new directory, but I don’t know how to call the code to make it work. Grateful for your help of what I am missing as nothing happens when I run the form at the moment.
-
Hi @janet01
I hope you’re well today!
Unfortunately, the file name doesn’t really tell me what code exactly are you using. Would you mind sharing the exact code (or a link to another topic from which you got that) with us?
To share a code, it’s best to put it at https://pastebin.com and just post a link to it in response there. Alternatively, simply post the link to the topic you took the code from.
Once we know that, we can check the code to make sure it’s working and provide you with additional information on how to make use of it.
Kind regards,
AdamHi Adam,
I was following this link:
https://wordpress.org/support/topic/get-number-of-days-from-two-date-pickers/
This sent me to this page of code:
https://gist.github.com/patrickfreitasdev/a0ce2af43c5bf24ef832cbf9744a5f14
Hi @janet01
Thanks for response and sharing the code
I checked it and it’s still a valid working code but needs to be configured correctly. Let me guide you through this step by step.
1. if you have added that code already on site anywhere, make sure to remove it first so we could start from scratch.
2. first, let’s make sure that form is configured correctly:
a) you need to have two “datepicker” type fields already on form, let’s say it’s “date-1” and “date-2”
b) make sure that both these fields have the same date format set
c) there should also be a “number” type field (let’s say it’s “number-1”
d) now go to the form’s “Behavior” settings and make sure that the “Load form using AJAX” option is disabled – this is very important here
3. create an empty file with a php extension (e.g. “forminator-days-diff.php”) and paste entire code into it
4. now let’s configure code
a) in this line
let _form_id = 1211,replace number with form ID; form ID is the number you see in form’s shortcode
b) in these two lines set your date fields id’s accordingly (if they are different than date-1 and date-2)
_date_field_id1 = 'date-1', _date_field_id2 = 'date-2',c) in this line set your number field ID (if it’s different than “number-1”)
_days_field_id = 'number-1',//or enter text-field idd) and in this line you need to set correct date separator:
_separated_date = '/',So if your date format set in datepicker fields is e.g. m/d/Y, separator would be / (as set) but if your date format is e.g. d-m-Y, then you need to replace / with –
e) finally, since you want to use this number in calculations, you also need to replace this line (near the end of the code)
_form.find('[name="'+ _days_field_id +'"]').val( Math.round( Math.abs( _date2 - _date1 ) / 86400000 ) );with this
_form.find('[name="'+ _days_field_id +'"]').val( Math.round( Math.abs( _date2 - _date1 ) / 86400000 ) ).change();5. now save the file and upload it to the “/wp-content/mu-plugins” folder of your site’s WordPress installation.
You may need to clear all cache on site/server but after that it should work out of the box if all those steps were followed.
Best regards,
AdamWorks fine, thanks!
Hi @janet01,
I hope you are doing well today!
We are happy to hear that the issue has been resolved and marking this thread accordingly.
Please let us know in case you need further help.Kind regards,
ZaferHi, Further to the above, I have realised I need to hide the field the date calculation goes into, otherwise the person filling in the form can change/overwrite the number of days. I found a post requesting similar help, and they were using css on the form with the code:
forminator-field-number-1 {
visibility: hidden;
}I have tried using the same, but it is my first time using css, and not sure if I have done the right thing, or put it in the right place? I went to Forminator Appearance, turned on custom CSS and pasted the code in there. I changed the example code’s field number from number-2 to number-1 which is the name of the field I want to hide.
Or perhaps there is a better way to hide the field?
Thanks for your help
Hello @janet01,
It looks like the code has to be updated to work correctly with the newer Forminator versions.
Could you please try using the following CSS rule instead, for hiding the field:
#number-1 { visibility: hidden; }Let us know if there’s still any issue.
Best Regards,
DmytroThat works fine, thanks Dmytro!
All the best, Janet
Hi, I hope you’re doing good today.
I need some help I think.
I followed all the tutorial to get the difference between dates and works perfect in my form but just when the site is displayed in a Computer, big screen. I does not work on responsive, small screen. The form is not displayed at all. Is there any relation between the code and the adaptability?
I also cleaned cache site/server
In advance, very thanks!
Hi @fergsarroca
Thank you for response!
There should be no relation between shared code/solution and form responsiveness or whether it shows on small screens or not.
Maybe we are missing something but I’d rather suspect some other issues, more specific to the site itself.
Would you mind sharing a link to the example page with the form on your site where we could see it happening?
Best regards,
Adam@wpmudev-support8
Thanks for your quick answer! Sure, here you have.https://europesurfschool.com/contact/
Works fine on pc, not in responsive.
On the other hand, is there any way to limit a number field with the data from another number field?
Thanks again (L)
-
This reply was modified 2 years, 3 months ago by
fergsarroca.
Hi @fergsarroca,
I does not work on responsive, small screen. The form is not displayed at all.
I could notice the form isn’t loading, however, it might be more related to the minification. Could you temporarily disable the minification in the Litespeed plugin and then check how it behaves?
If it works, clearing the cache should resolve on the plugin side. Would also recommend checking whether enabling the “Load form using AJAX” option makes any difference or not. If this option is currently off, attempt switching it on or performing the opposite action if it is already enabled. Test your site’s functionality throughout these adjustments to monitor for any changes.
The option can be found when you edit your form under “Behaviour” tab > Rendering
If the issue is still the same even after the above then please open a new thread referring to this thread, so that we can investigate further.
Since this thread is initially opened by a different user, troubleshooting in this thread can cause miscommunication.
Please use the following URL to open a new thread:
https://wordpress.org/support/plugin/forminator/#new-topic-0I hope you understand.
Regards,
Nithin
-
This reply was modified 2 years, 3 months ago by
The topic ‘forminator, mu-plugins – difference between two dates’ is closed to new replies.