Using Gravity Forms ‘gform_date_max_year’ PHP filter

The gform_date_max_year filter in Gravity Forms allows you to specify the maximum year displayed in the date field’s year drop-down list and the HTML5 max attribute for the date field’s year input.

Usage

add_filter('gform_date_max_year', 'set_max_year');

Parameters

  • $max_date (string) – The maximum date to be filtered. Defaults to the current year plus one.
  • $form (array) – Current form object.
  • $field (array) – Current field object.

More information

See Gravity Forms Docs: gform_date_max_year

Examples

Set the maximum year to 2022

This code sets the maximum year for all date fields to 2022.

add_filter('gform_date_max_year', 'set_max_year');
function set_max_year($max_date) {
    return 2022;
}

Set the maximum year for a specific field on a specific form

This code sets the maximum year to 2022 for the date field with ID 5 on the form with ID 7.

add_filter('gform_date_max_year', function($max_date, $form, $field) {
    return ($form['id'] == 7 && $field->id == 5) ? 2022 : $max_date;
}, 10, 3);

Set the maximum year dynamically based on a custom option

This code sets the maximum year based on the value of a custom option named ‘custom_max_year’.

add_filter('gform_date_max_year', 'set_max_year_from_option');
function set_max_year_from_option($max_date) {
    $custom_max_year = get_option('custom_max_year');
    return $custom_max_year ?: $max_date;
}

Set the maximum year to the current year

This code sets the maximum year for all date fields to the current year.

add_filter('gform_date_max_year', 'set_max_year_to_current_year');
function set_max_year_to_current_year($max_date) {
    return date("Y");
}

Set the maximum year to a specific year for a form with a specific title

This code sets the maximum year to 2030 for date fields in forms with the title ‘Special Event Registration’.

add_filter('gform_date_max_year', function($max_date, $form, $field) {
    return ($form['title'] == 'Special Event Registration') ? 2030 : $max_date;
}, 10, 3);

Note: Place the code examples in the functions.php file of your active theme.