Only Allow First of the Month

This snippet will block all dates except the first date of each month.

Instructions

Code

Filename: gpld-only-allow-first-of-month.php

<?php
/**
 * Gravity Perks // Limit Dates // Only Allow First of the Month
 * https://gravitywiz.com/documentation/gravity-forms-limit-dates/
 *
 * Instruction Video: https://www.loom.com/share/69ac8a7d77f7439e8f758323b7cfc88d
 *
 * This snippet will block all dates except the first date of each month.
 */
// 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 = array();

	$current     = new DateTime();
	$month       = new DateInterval( 'P1M' );
	$target_year = (int) $current->format( 'Y' ) + 20;

	while ( true ) {
		$current->modify( 'first day of this month' );
		$exceptions[] = $current->format( 'm/d/Y' );
		$current->add( $month );
		if ( (int) $current->format( 'Y' ) === $target_year ) {
			break;
		}
	}

	$field_options['disableAll']    = true;
	$field_options['exceptionMode'] = 'enable';
	$field_options['exceptions']    = $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.