How to Calculate in Both Years and Months with Gravity Forms
Learn how to use Gravity Forms to calculate in both years and months for countdowns, job histories, exact ages, or anything else.
This article requires GP Date Time Calculator and GP Advanced Calculations.
Buy Gravity Perks today and get 46+ premium Gravity Forms plugins, including these perks!
Ever needed to use Gravity Forms to calculate a period of time in both years and months?
This is useful when calculating job histories, children’s ages, a countdown, or anywhere that you need an up-to-the-month calculation.
In this tutorial, we’re going to use Gravity Forms Date Time Calculator and Gravity Forms Advanced Calculations to count the number of years and months between two dates.
Steps
Step 1 – Add fields to your form
Start by adding two Date fields and two Number fields to the form. Call the Date fields “Start Date” and “End Date”. Name the Number fields “Years” and “Months”.
Step 2 – Years calculation
With the fields in place, we’ll start with the years calculation. Click Enable calculation in the “Years” field settings, and subtract “End Date” from “Start Date”. Wrap that calculation in parentheses, and add floor
in front of the opening parenthesis.
The floor
function is included with Advanced Calculations and automatically rounds a number down to the nearest integer. Your final calculation should look like this:
floor( {End Date:3} - {Start Date:1} )
Next, set Rounding to 0
and Date Calculation Unit to Years. Here’s what the whole setup looks like:
For our more frugal readers, an alternative for calculating the mathematical floor is to subtract 0.5 from the result and set the Rounding to 0. The calculation looks like this: ( {End Date:3} – {Start Date:1} ) – 0.5
Step 3 – Months calculation
The months calculation is similar to the years calculation, however we’re going to subtract the years calculation results to only return the remainder of months.
floor( {End Date:3} - {Start Date:1} ) - ( {Years:4} * 12 )
Notice we multiply the {Years:4}
merge tag by 12
. This converts it to months. Same as before, set Rounding to 0
. For this field, the Date Calculation Unit is Months.
That’s It
With the calculations in place, our form now calculates years and months together. Thanks to the floor functions, our calculations are accurate and delivered in whole numbers.
Questions?
Have questions or comments about this tutorial? We’d love to hear them! 👇
I would like to buy the unlimited perks, My question is when I unsubscribe, will I loose all what I did with the plugins or just support and updates? Also is there a way to do sum of a column in a field on conditions, like if Field 1 is A then accumulate Field 2 and pupulate Field 3
Hi Nurelegn,
If your license expires, the plugins will still function. You will no longer be able to manage or update your Perks, but they’ll continue to work as long as they are installed and active.
Regarding your second question, I think we’ll need some more information to help with that. Can you drop us a line so our Support Wizards can assist?