Date operation

In this article we will describe how to make some calculations with Dates.

Without custom scripting, you can use the Form Calculations to calculate how many minutes, hours or days have passed between two calendars or date time picker elements. You can read more about this topic here.

Example 1: Adding a number of days to the current date

The following fields will need to be created:

Field1 - text box with this code to the default value:

//<code>
return date('m/d/o');
//</code>

This snippet will return the current date. Of course you can also use the calendar to select a specific date.

Field2 - text box with "Numeric" validation rule

Field3 - hidden field

For the next step you will have to set the Field3 value with the result of the desired calculation. To achieve this just paste the following code in the "Scripts" tab, on "Scripts called on form process":


$_POST['form']['Field3'] = date('m/d/Y',strtotime(' +'.$_POST['form']['Field2'].' days'));

To display this result in the front-end just use the appropriate placeholder (eg. {Field3:value}).

Example 2: Count number of days between 2 dates

Date1 - text box with this code to the default value(this will return the current date):

//<code>
return date('m/d/o');
//</code>

Date2 - text box with this code to the default value(this will return a date in the future)

//<code>
return date('2/23/2022');
//</code>

Result - hidden field which will retrieve the number of days between the two dates

In order to calculate the number of days between Date2 and Date1, just paste the following code in the "Scripts" tab, on "Scripts called on form process":


$first_date = strtotime($_POST['form']['Date1']);
$second_date = strtotime($_POST['form']['Date2']);
$result = $second_date - $first_date;

$_POST['form']['Result'] = floor($result / (60*60*24) ) ." days";

To display this result in the front-end just use the appropriate placeholder (eg. {Result:value}).


13 persons found this article helpful.


Was this article helpful?

Yes No
Sorry about that

You Should Also Read

How to calculate the difference in days between two calendar dates. HOT