Check that one date field is not before a previous date field

I have a form, form 9, with two date fields, 18 and 11. I want to check, when the entry is updated (I am using GravityView), to make sure that date 11 is not before date 18.

I have this code, but it’s only referencing looking at 18 to make sure it’s not the same as 11, not looking at before.

add_filter( ‘gform_field_validation_9_18’, function ( $result, $value, $form, $field ) {
$master = rgpost( ‘input_9_11’ );
if ( $result[‘is_valid’] && $value == $master ) {
$result[‘is_valid’] = false;
$result[‘message’] = ‘Return date is before Start date.’;
}

return $result;

}, 9, 1 );

Anyone tackled this type of thing before? Thanks in advance!

Give this a try. It converts both dates to a timestamp and compares the values.

add_filter( 'gform_field_validation_9_8', function ( $result, $value, $form, $field ) {

    $master = rgpost( 'input_9_11' );

    $master_timestamp = ( DateTime::createFromFormat( 'm/d/Y', $master ) )->format('U');
    $value_timestamp = ( DateTime::createFromFormat( 'm/d/Y', $value ) )->format('U');

    if ( $result['is_valid'] && $value_timestamp <= $master_timestamp ) {
        $result['is_valid'] = false;
        $result['message'] = 'Return date is before Start date.';
    }

    return $result;

}, 9, 4 );
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.