Yearly Recurring Exceptions

This snippet will make all excepted dates recur yearly.

Instructions

Code

Filename: gpld-recurring-exceptions-yearly.php

<?php
/**
 * Gravity Perks // Limit Dates // Yearly Recurring Exceptions
 * https://gravitywiz.com/documentation/gravity-forms-limit-dates/
 *
 * Instruction Video: https://www.loom.com/share/9f9fcfa2901d46cabd319ac9ada2d1ca
 *
 * This snippet will make all excepted dates recur yearly.
 */
// Update "123" to your form ID. Update "4" to your Date field ID.
add_filter( 'gpld_limit_dates_options_123_4', function( $field_options, $form, $field ) {

	$exceptions = $field_options['exceptions'];
	if ( empty( $exceptions ) ) {
		return $field_options;
	}

	$future_exceptions = array();

	foreach ( $exceptions as $exception ) {

		$current     = new DateTime( $exception );
		$year        = new DateInterval( 'P1Y' );
		$target_year = (int) $current->format( 'Y' ) + 20;

		while ( true ) {
			$current->add( $year );
			$future_exceptions[] = $current->format( 'm/d/Y' );
			if ( (int) $current->format( 'Y' ) === $target_year ) {
				break;
			}
		}
	}

	$field_options['exceptions'] = array_merge( $exceptions, $future_exceptions );

	return $field_options;
}, 10, 3 );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.