Select Page

Formidable date field offset value

[Update] Formidable Forms now supports “offset” attribute. So this custom code is no longer required.

Formidable Forms [date] shortcode is used to get the current date. If a date relative to the current date is required, the offset attribute can be used.
e.g. [date offset='+3 weeks'] will return the date three weeks from today.

However, this is not true when a date field value is displayed using [x] shortcode where x is the field id or key. The [x] shortcode doesn’t support the offset attribute. But it is very easy to extend it with a few lines of custom code.

Here are some examples for a date field with id 123

[123 offset='+2 weeks']
[123 offset='-1 month']
[123 offset='+1 year 3 weeks']
[123 offset='Next Monday']
[123 offset='Last Friday']
[123 offset='First day of this month']
[123 offset='Last day of next month']

The offset attribute can be used together with format attribute which is already supported by Formidable.
e.g. [123 offset='+2 weeks' format='m/d/Y']
Replace 123 with your field ID.

Add the following code to your functions.php or use Code Snippet plugin to enable this functionality.

add_filter('frmpro_fields_replace_shortcodes', 'frm_city_date_offset', 10, 4);
function frm_city_date_offset( $replace_with, $tag, $atts, $field ) {
    if ($field->type == 'date' and isset($atts['offset']) and $replace_with) {
		$replace_with = date('Y-m-d', strtotime($replace_with . $atts['offset']));
    }
    return $replace_with;
}

Formidable date field Cheat sheet

Learn all about Formidable Forms date field in this Cheat sheet.

Field Type – Date
This date field requires a value. If no value provided, there will be a validation error message (customizable). The default “required field indicator” is * (customizable).
This is a read-only date field. User can’t choose any options here. However, options can be set indirectly using “Default Value” in field settings or programmatically.
Here date field’s default value is set to a specific date (2020-01-01)
The current date can be set with [date] shortcode. An offset can be used to set a date relative to current date.
e.g. [date offset=’+2 weeks’]
e.g. [date offset=’-1 month’]
e.g. [date offset=’+1 year 3 weeks’]
e.g. [date offset=’Next Monday’]
e.g. [date offset=’Last Friday’]
e.g. [date offset=’First day of this month’]
e.g. [date offset=’Last day of next month’]
This date field only allows selecting Mon, Tue, and Wed. This requires the “Datepicker Options” add-on.
This date field doesn’t allow to select “2019-09-24” and “2019-09-25”. This requires the “Datepicker Options” add-on.
This date field allows selecting a date in the current year and next 3 years.
This date field allows a date between today and 30 days from today. This requires the “Datepicker Options” add-on.
There are multiple ways of setting a min and a max of the date range allowed. They are
  • A specific date. e.g. 2020-01-01. As of writing this, this can’t be dynamically set using a shortcode such as [date offset=’+2 weeks’]
  • Current date with an optional offset. Offset only supports +/- n days/weeks/months/years format. Other formats like “Next Monday”, “First day of this month” are not supported as of writing this.
  • A date from another date field with an optional offset (Refer the example below). Offset only supports +/- n days/weeks/months/years format. Other formats like “Next Monday”, “First day of this month” are not supported as of writing this.
Following example shows how a date field’s date range can be controlled from another date field.

The “travel start date” allows selecting today or a future date. The “travel end date” allows selecting a date from the next day of travel start date until a month from the travel start date.
This requires custom coding with frm_date_field_options hook.