I also have this question. 🙂
I need to subtract 2 dates to find the number of months. how do I do that?
You’ll need to update the caldera-forms/fields/calculation/field.php file.
Change this line (~62)
$binds_vars[] = $fid . " = parseFloat( $('[data-field=\"" . $fid . "\"]').is(':checkbox') ? checked_total_" . $field_base_id . "($('[data-field=\"" . $fid . "\"]:checked')) : $('[data-field=\"" . $fid . "\"]').is(':radio') ? $('[data-field=\"" . $fid . "\"]:checked').val() : $('[data-field=\"" . $fid . "\"]').val() ) || 0 ";
With this :
if($cfg['type'] == 'date_picker') {
$binds_vars[] = $fid." = ((new Date( $('[data-field=\"".$fid."\"]').val() )) / (1000 * 3600 * 24))";
} else {
$binds_vars[] = $fid . " = parseFloat( $('[data-field=\"" . $fid . "\"]').is(':checkbox') ? checked_total_" . $field_base_id . "($('[data-field=\"" . $fid . "\"]:checked')) : $('[data-field=\"" . $fid . "\"]').is(':radio') ? $('[data-field=\"" . $fid . "\"]:checked').val() : $('[data-field=\"" . $fid . "\"]').val() ) || 0 ";
}
This will convert your date to a number of days. So when you substract dates, you’ll get number of days between them.
You also have to change the Caldera_Forms::run_calculation() function in caldera-forms/classes/core.php (~1017)
Search for this : (~1049)
if (is_array($entry_value)) {
$number = floatval(array_sum($entry_value));
} else {
$number = floatval($entry_value);
}
To change it for :
if($cfg['type'] == 'date_picker') {
$number = empty($entry_value) ? 0 : (strtotime($entry_value) / (3600 * 24));
} else {
if (is_array($entry_value)) {
$number = floatval(array_sum($entry_value));
} else {
$number = floatval($entry_value);
}
}
Forked it in with the fix my Github :
https://github.com/pboissonneault/Caldera-Forms
I tried the above changes, however it did not work and it broke all calculation. Do you perhaps have a solution for the latest version of the plugin (Version 1.4.3.1)