Skip to content

Instantly share code, notes, and snippets.

@cliffordp
Last active April 14, 2026 02:02
Show Gist options
  • Select an option

  • Save cliffordp/38892bbfe30702d4623c9c13e17a1b09 to your computer and use it in GitHub Desktop.

Select an option

Save cliffordp/38892bbfe30702d4623c9c13e17a1b09 to your computer and use it in GitHub Desktop.
Disable USA Holidays for Gravity Forms Gravity Wiz Limit Dates.
<?php
/**
* Disable USA Holidays for Gravity Forms Gravity Wiz Limit Dates.
*
* @link https://gravitywiz.com/documentation/gravity-forms-limit-dates/
* @link https://gist.github.com/cliffordp/38892bbfe30702d4623c9c13e17a1b09 This snippet.
*
* DOES NOT appear in the form editor's UI but DOES take effect when filling the form.
* If you add your own Exception within form editor's UI, that will take precedence over these rules.
*
* TODO: ADD YOUR OWN FORM#s AND FIELD#s AT THE BOTTOM!
*/
if ( ! class_exists( 'GW_Limit_Dates_Federal_Holidays' ) ) {
class GW_Limit_Dates_Federal_Holidays {
// Set how many years in advance to generate
private int $years_to_generate = 2;
// Internal cache so we only calculate dates once per page load
private ?array $generated_holidays = null;
/**
* Constructor
* @param array $targets Array of strings in "formId_fieldId" format.
*/
public function __construct( array $targets ) {
foreach ( $targets as $target ) {
add_filter( "gpld_limit_dates_options_{$target}", [ $this, 'apply_holiday_exceptions' ], 10, 3 );
}
}
/**
* The callback that intercepts the field options and applies the dates.
*/
public function apply_holiday_exceptions( $field_options, $form, $field ) {
if ( $this->generated_holidays === null ) {
$this->generated_holidays = $this->generate_dates();
}
// Safely merge our federal holidays with any exceptions you clicked in the UI
$existing_exceptions = $field_options['exceptions'] ?? [];
$field_options['exceptions'] = array_values( array_unique( array_merge( $existing_exceptions, $this->generated_holidays ) ) );
return $field_options;
}
/**
* The math engine that figures out the US Federal Holidays & Observations.
*/
private function generate_dates(): array {
$federal_holidays = [];
$current_year = (int) wp_date( 'Y' );
for ( $i = 0; $i <= $this->years_to_generate; $i++ ) {
$year = $current_year + $i;
// 1. Fixed Dates (Require observation math)
$fixed_dates = [
"$year-01-01", // New Year's Day
"$year-06-19", // Juneteenth
"$year-07-04", // Independence Day
"$year-11-11", // Veterans Day
"$year-12-25", // Christmas Day
];
foreach ( $fixed_dates as $date_string ) {
$timestamp = strtotime( $date_string );
$day_of_week = (int) gmdate( 'w', $timestamp ); // 0 = Sunday, 6 = Saturday
// Shift weekends to observed Federal weekdays
if ( $day_of_week === 0 ) {
// Sunday -> Observe on Monday
$timestamp = strtotime( '+1 day', $timestamp );
} elseif ( $day_of_week === 6 ) {
// Saturday -> Observe on Friday
$timestamp = strtotime( '-1 day', $timestamp );
}
$federal_holidays[] = gmdate( 'm/d/Y', $timestamp );
}
// 2. Floating Dates (Always land on a weekday natively)
$floating_dates = [
"third monday of january $year", // Martin Luther King Jr. Day
"third monday of february $year", // Washington's Birthday / Presidents' Day
"last monday of may $year", // Memorial Day
"first monday of september $year", // Labor Day
"second monday of october $year", // Columbus Day / Indigenous Peoples' Day
"fourth thursday of november $year"// Thanksgiving Day
];
foreach ( $floating_dates as $date_string ) {
$federal_holidays[] = gmdate( 'm/d/Y', strtotime( $date_string ) );
}
}
return $federal_holidays;
}
}
}
// --------------------------------------------------------
// USAGE: Instantiate the class and pass in your targets
// Format: "formId_fieldId"
// --------------------------------------------------------
new GW_Limit_Dates_Federal_Holidays( [
'5_15', // Applies to Form 5, Field 15
'1_15', // Applies to Form 1, Field 15
] );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment