calculation based on dates

I have an arrival and and a departure date and  want to automatically calculate the number of nights spend in an etablishment. this does not work correctly. When I select august 30 and september 6 , the result is -24!

How should I accomplish this? 
gideon

The built-in calculations do not cover dates. This would probably be best to do in php using the
frm_validate_field_entry hook. You can add some custom code to a new
plugin or your theme functions.php. Something like this:

add_filter('frm_validate_field_entry', 'calculate_time', 11, 3);
function calculate_time($errors, $field, $value){
if($field->id == 25){ //change 25 to the ID of the hidden or admin only field you have added to hold the calculation
global $frmpro_settings;
  $start = $_POST['item_meta'][23]; //change 23 to the ID of the first field
  $end = $_POST['item_meta'][24]; //change 24 to the ID of the first field

if (!preg_match('/^d{4}-d{2}-d{2}$/', trim($start)))
$start = FrmProAppHelper::convert_date($start, $frmpro_settings->date_format, 'Y-m-d'); //convert date format

if (!preg_match('/^d{4}-d{2}-d{2}$/', trim($end)))
$end = FrmProAppHelper::convert_date($end, $frmpro_settings->date_format, 'Y-m-d'); //convert date format
  $calc = round((strtotime($end)-strtotime($start))/86400); //number of days between dates
  $value = $_POST['item_meta'][25] = $calc;
}
return $errors;
}

how would I go about adding this to a new plugin?

I want to make it in such a way that updates of the theme and or your plugin dont make it dissapear.

Stephanie or anybody else knows how to make a custom plugin for this?

You can find instructions on creating a new plugin in the WordPress codex.
http://codex.wordpress.org/Writing_a_Plugin

Basically, create a file and include the WP header info at the top.

<?php
/*
Plugin Name: Name Of The Plugin
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: A brief description of the Plugin.
Version: The Plugin's Version Number, e.g.: 1.0
Author: Name Of The Plugin Author
Author URI: http://URI_Of_The_Plugin_Author
License: A "Slug" license name e.g. GPL2
*/
?>


Since we are not general WordPress support, please use Google and the WP community to help answer any questions you may have about creating a plugin. Thanks!

I am setting up an Family Reunion Event but I need every event to be calculated when selected for my users and calculate their total. Is there a way I can do this with this form?

I'm guessing you're calculating a price per the number of days? This can be done, but would require custom code similar to what is seen above.

I have a similar question.  I added the same code above and it appears to have given a correct day calculation but for some reason no matter when I add another item to the date calculation it always returns only the number for the date calculation.  I am trying to get that date range to be multiplied by 5.  Example if there were 3 days it should return 15 since 3 days times 5 equals 15 but it will only return 3 which is the difference in date fields. 

I have tried: ([101]-[100])*[87]*5  and ([101]-[100])*5 but am only getting the difference between the 101 and 100 date fields. 
Any thoughts? 

Form below:
http://www.sdfairportparking.com/index.php?plugin=formidable&controller=forms&action=preview&form=2y7e9q

Thanks.

The default calculations only support numbers, not date calculations. This is the reason for the custom code above. The two should not be combined for the calculation of the same field because you will get different results from each.

I guess I am confused now.  I thought I was on my way to success.  So you are saying there is no way to accomplish this then?  No way to get the date calculation difference numerically and then multiply or add that numeric difference with another number or field?

Not with the built-in calculations. Instead, you can use custom code similar to what is shown above to perform your calculation.

Maybe I am not explaining myself enough.  I used that code above and put it in the functions code of the theme.  The date calculation appears to have worked correctly as it was giving me the correct difference by itself but when I tried to calculate that difference with another value like multiplying by 5 or adding 5 for example it still just returns the value difference in date. 

You are inserting calculations for the date fields in the calculation line in the advanced field options. This is not supported in the current version. If you'd like to modify the calculation you must do it in your custom PHP function. It will not work correctly to use any date fields in a calculation in the field options.

If I add the above custom code to the functions.php I can't create a hidden field in a form and post the calculation to it?  Then add to the paypal calculation the new hidden field id and multiply or add to it?  The way I was reading your sample code made it seem like it was holding the date calculation.

if($field->id == 25){ //change 25 to the ID of the hidden or admin only field you have added to hold the calculation


Here is the code I added to my theme:

// Form Calculations
add_filter('frm_validate_field_entry', 'calculate_time', 11, 3);
function calculate_time($errors, $field, $value){
if($field->id == 115){ //change 25 to the ID of the hidden or admin only field you have added to hold the calculation
global $frmpro_settings;

$start = $_POST['item_meta'][100]; //change 23 to the ID of the first field
$end = $_POST['item_meta'][101]; //change 24 to the ID of the first field

if (!preg_match('/^d{4}-d{2}-d{2}$/', trim($start)))
$start = FrmProAppHelper::convert_date($start, $frmpro_settings->date_format, 'Y-m-d'); //convert date format

if (!preg_match('/^d{4}-d{2}-d{2}$/', trim($end)))
$end = FrmProAppHelper::convert_date($end, $frmpro_settings->date_format, 'Y-m-d'); //convert date format

$calc = round((strtotime($end)-strtotime($start))/86400); //number of days between dates
$value = $_POST['item_meta'][115] = $calc;
}
return $errors;
}


The new paypal calculation I attempted was:
[115]*[87]*5

It is not possible to combine the two different types of calculations in this way. The built-in calculations use javascript as the user interacts with the form. Your custom code uses PHP which is only fired when the form is submitted. The two do not interact. You cannot combine the two methods for the same calculation. It must be done entirely in PHP or entirely in javascript. It looks like the easiest solution would be to change the line:

$calc = round((strtotime($end)-strtotime($start))/86400);

to:

$calc = round((strtotime($end)-strtotime($start))/86400) * $_POST['item_meta'][87] * $_POST['item_meta'][5];


Thank you for the help.  It is very much appreciated.

Topic closed.