Viewing 1 replies (of 1 total)
  • Plugin Author doublesharp

    (@doublesharp)

    Yes, it can validate a date using the PHP validation – really you can do pretty much anything you want with the PHP using the $value argument. StackOverflow is a great place to look for help with questions like these, but all you need to do is convert the String to a Date using strtotime() and then do the comparison using the results of date().

    // make sure the date isn't empty and is in the format yyyy-dd-mm or change for your format
    if ( empty( $value ) || ! preg_match( '~\d{4,4}-\d{2,2}-\d{2,2}~", $value){
        $message = 'You must enter a valid date';
        return false;
    } else {
        $input_date = strtotime( $value ); // input date in seconds
        $today_str = date( 'Y-m-d' );      // date as string based on current timestamp
        $today = strtotime( $today_str );  // today's date on the server in seconds for comparison
        if ( $today < $input_date ){
            $message = 'The date cannot be in the future. Today is ' . $today_str . ' and you entered ' . $value;
            return false;
        } elseif ( $today == $input_date ){
            return true; // the date is today
        } elseif ( $today > $input_date ){
            return true; // the date is in the past
        }
    }
Viewing 1 replies (of 1 total)
  • The topic ‘Date validation’ is closed to new replies.