Can I use [gravitymath] with dates and times?
Yes, you can use GravityMath to perform calculations with dates and times. This makes it possible to calculate durations, such as the time between when an entry was created and last updated.
For example, the following shortcode snippet calculates the difference between the entry’s date updated and date created, displaying the result in days, hours, and minutes:
[gravitymath scope="entry"] floor( max(0, ({date_updated:timestamp}+0) - ({date_created:timestamp}+0)) / 86400 ) [/gravitymath]d [gravitymath scope="entry"] floor( ( max(0, ({date_updated:timestamp}+0) - ({date_created:timestamp}+0)) - floor( max(0, ({date_updated:timestamp}+0) - ({date_created:timestamp}+0)) / 86400 ) * 86400 ) / 3600 ) [/gravitymath]h [gravitymath scope="entry"] floor( ( max(0, ({date_updated:timestamp}+0) - ({date_created:timestamp}+0)) - floor( max(0, ({date_updated:timestamp}+0) - ({date_created:timestamp}+0)) / 3600 ) * 3600 ) / 60 ) [/gravitymath]m
This outputs the elapsed time since an entry was created in the format:
Xd Yh Zm
You can add this shortcode to a Custom Content field in your View to display the calculation directly alongside other entry data.
How it works
Gravity Forms stores dates as timestamps, which are large numbers representing the number of seconds since January 1, 1970. GravityMath subtracts one timestamp from another to calculate elapsed time.
- Days: There are 86,400 seconds in a day (24 hours × 60 minutes × 60 seconds). The calculation divides the total seconds by 86,400 to return whole days.
- Hours: After removing the days, the calculation divides the remaining seconds by 3,600 (60 minutes × 60 seconds) to return whole hours.
- Minutes: After removing the days and hours, the calculation divides the leftover seconds by 60 to return whole minutes.
The floor()
function rounds down to the nearest whole number so the result never includes partial days, hours, or minutes.
Tip: You can adapt the shortcode to work with other date or time fields in your form.