Formidable Forms has a built-in feature that will allow you to display a saved date as a relative number of hours, days, weeks, months or years ago.
The basic usage is [x time_ago=1]
where x is the field code and 1 is the type or number of levels of detail, as described here.
I needed to output the number of days ago in Arabic. Using [x time_ago=day]
outputs “yyy days”, but there doesn’t appear to be a way to modify the word “days” if the strings haven’t been translated and approved in a particular language or if you need to use a language other than your site’s default or active language.
If you’re just using a single unit (e.g. days) like me, here’s a coding-free workaround by combining built-in shortcodes.
Steps
Remove default units
The first step is to strip away the units. This can be done using the [frm-math]
maths shortcode and inserting the option clean=1
which removes any non-numerical characters.
[frm-math clean=1] [x time_ago=day] [/frm-math]
Save as param
The second step is to save this as a parameter (param) ready for the next step. Although you can pass frm-math directly to [frm-condition]
, there’s no way to combine it with clean=1).
[frm-set-get param=daysago] [frm-math clean=1] [x time_ago=day] [/frm-math] [/frm-set-get]
Add custom units
The final step is to add back in an alternative unit, with appropriate grammar applied using Formidable’s conditional shortcode. In Arabic units we have singular (1), dual (2), plural (3-10), and then back to singular units for numbers 11 and over.
Singular (١ يوم)
[frm-condition source=param param=daysago equals=1] [frm-math clean=1] [x time_ago=day] [/frm-math] يوم[/frm-condition]
Dual (يومين)
[frm-condition source=param param=daysago equals=2]يومين[/frm-condition]
Plural (٣ أيام – ١٠ أيام)
[frm-condition source=param param=daysago greater_than=2 less_than=11][frm-math clean=1] [x time_ago=day] [/frm-math] أيام[/frm-condition]
Over 10 (١١ يوم وما فوق)
[frm-condition source=param param=daysago greater_than=10][frm-math clean=1] [x time_ago=day] [/frm-math] يوم[/frm-condition]
Putting it all together
Combining this all together, and removing extra line breaks to keep the output tidy, we get:
[frm-set-get param=timeago][frm-math clean=1][x time_ago=day][/frm-math][/frm-set-get][frm-condition source=param param=daysago greater_than=2 less_than=11][frm-math clean=1][x time_ago=day][/frm-math] أيام[/frm-condition][frm-condition source=param param=daysago equals=1][frm-math clean=1][x time_ago=day][/frm-math] يوم[/frm-condition][frm-condition source=param param=daysago equals=2]يومين[/frm-condition][frm-condition source=param param=daysago greater_than=10][frm-math clean=1] [x time_ago=day][/frm-math] يوم[/frm-condition]