• Hi, I’ve found a way to check date and time and set a minimum time filter, for booking for example, and would like to share with you. If there is any error, please say to me.

    First of all, in the form, we add date picker and two selects for hours and minutes:

    <p>Date[date date1 id:date-form "yyyy-mm-dd"] </p>
    <p>Hour * [select* hour1 id:hour1 "00" "01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "20" "21" "22" "23"]
    Minutes * [select* minute1 id:minute1 "00" "05" "10" "15" "20" "25" "30" "35" "40" "45" "50" "55"]</p>

    In functions.php, we add the filter. In the example we set a minimum of 24 hours:

    //Contact Form 7 date and time validator
    add_filter( 'wpcf7_validate_select*', 'custom_datetime_confirmation_validation_filter', 10, 2 );
    
    function custom_datetime_confirmation_validation_filter( $result, $tag ) {
        $tag = new WPCF7_Shortcode( $tag );
    
        if ( 'minute1' == $tag->name ) {
            // Booking date and time
            $minute1 = isset( $_POST['minute1'] ) ? trim( $_POST['minute1'] ) : '';
            $hour1 = isset( $_POST['hour1'] ) ? trim( $_POST['hour1'] ) : '';
            $date1 = isset( $_POST['date1'] ) ? trim( $_POST['date1'] ) : '';
            $date1 = explode('-', $date1);
            // Convert date and time to minutes
            $summinres = ($date1[0] * 525600) + ($date1[1] * 43200) + ($date1[2] * 1440) + ($hour1 * 60) + $minute1;
    
            // Actual date and time
            $hoy = getdate();
            // Convert date and time to minutes (in UTC+2)
            $summinhoy = ($hoy[year] * 525600) + ($hoy[mon] * 43200) + ($hoy[mday] * 1440) + (($hoy[hours]+2) * 60) + $hoy[minutes];
    
          // Month with 31 days
             if ($hoy[mday]==31) {
                $summinhoy -= 1440;
            };
            // February and leap-year check
            if ($hoy[mon]==2) {
                if(((!($hoy[year]%4) && ($hoy[year]%100)) || !($hoy[year]%400)) && ($hoy[mday]==29)) {
                    $summinhoy += 1440;
                } elseif ($hoy[mday]==28){
                    $summinhoy += 2880;
                }
            };
            // Difference
            $difference = $summinres - $summinhoy;
    
            // Minimum time (24 hours)
            $compare = 1440;
    
            // Comparison
            if ( $difference < $compare ) {
                $result->invalidate( $tag, "Minimum booking of 24 hours" );
            }
        }
        return $result;
    }

    I hope it helps. And sorry about my English.

    https://wordpress.org/plugins/contact-form-7/

  • The topic ‘Include minimum date and time filter’ is closed to new replies.