Skip to content
Advertisement

PHP Make form field read-only conditionally based on date & time

I’m trying to have 2 fields in my form become read only after a certain time & date. I’ve cobbled together some code from a few places in an attempt to make this work but I’m a total novice so I can’t identify where it’s going wrong or why.

The fields I want conditionally read-only are 1 & 13 in the example below. I’m not even sure what the numbers 9 & 2 are for. Like I said, absolute beginner.

Adding Angel’s time & date definitions from his answer, here’s what I have so far…

$W1F1Start = "2020-10-16 12:42:00";
  $now = date('Y-m-d H:i:s');
 if(strtotime($now) > strtotime($W1F1Start)){

add_filter('frm_setup_new_fields_vars', 'frm_set_read_only_on_create', 9, 2);
add_filter('frm_setup_edit_fields_vars', 'frm_set_read_only_on_create', 9, 2);
function frm_set_read_only_on_create( $values, $field ){
 
  if ( in_array( $field->id, array( 1,13 ) ) ) { 
       $values['read_only'] = 1;
    }
 return $values;
}
return $values;
}

If I get this to work, I eventually need to make up to 20 fields read-only at different times and dates in the same form.

My form is a Formidable form on wordpress if it makes a difference.

In their knowledge bank they have 2 bits of code that I assume I can put together somehow to do what I want, but I can’t figure out how…

This one ‘makes fields read only’ based on a thing

add_filter('frm_setup_new_fields_vars', 'frm_set_read_only_on_create', 9, 2);
add_filter('frm_setup_edit_fields_vars', 'frm_set_read_only_on_create', 9, 2);
function frm_set_read_only_on_create( $values, $field ){
    // If on the back-end, keep fields editable
    if ( FrmAppHelper::is_admin() || current_user_can( 'administrator' ) ) {
      return $values;
    }

    // If on front-end, make specific fields read-only
        if ( in_array( $field->id, array( 554,555,556 ) ) ) { 
       $values['read_only'] = 1;
    }   
    return $values;
}

And this one does something ‘based on current date’

add_filter('frm_setup_new_fields_vars', 'remove_field_option_by_date', 30, 2);
add_filter('frm_setup_edit_fields_vars', 'remove_field_option_by_date', 30, 2);
function remove_field_option_by_date( $values, $field ) {
  $today = time();
  $close_date = strtotime ( "2020-06-20" ); // change 2020-06-20 to the date after which the option should be removed
  
  if ($today id == 13677 ) { // change 13677 to your field id
    $options_to_remove = array( 'Option 1' ); // change Option 1 to the value to remove
    foreach ( $options_to_remove as $remove ) {
      $option_key = array_search( $remove, $values['options'] );
      if ( $option_key !== false ) {
        unset( $values['options'][ $option_key ] );
      }
    }
  }
  return $values;
}

I need to ‘make fields read only’-‘based on a date’. But I’m at a loss as to how to put those two things together.

Hope this is all the information needed. Is this possible?

Advertisement

Answer

1st – in WordPress “code snippets” go in functions.php, located inside the theme you’re using, so can do this without a plugin. Now to compare 2 dates you can have something like this:

// Static W1F1 start date
$W1F1Start = new DateTime();
$W1F1Start->setTimestamp(1602840600);
$now = new DateTime();
    
if($now > $W1F1Start){
  // do whatever
}

Or lets assume your variable $W1F1Start is a string “2020-10-16 09:30:00” (this is the value of formatted $W1F1Start->format(‘Y-m-d H:i:s’)) and you want to compare that to what current time, then you can use strtotime(), which will convert your strings to timestamps:

$W1F1Start = "2020-10-16 09:30:00";
$now = date('Y-m-d H:i:s');

if(strtotime($now) > strtotime($W1F1Start)){
   // do whatever
}

So this is your code final look:

add_filter('frm_setup_new_fields_vars', 'frm_set_read_only_on_create', 9, 2);
add_filter('frm_setup_edit_fields_vars', 'frm_set_read_only_on_create', 9, 2);
function frm_set_read_only_on_create( $values, $field ){

    // Static W1F1 start date
    $W1F1Start = new DateTime();
    $W1F1Start->setTimestamp(1602840600);
    // current time
    $now = new DateTime();
    $now->setTimezone(new DateTimeZone('UTC'));

    // if cuurent time > W1F1Start    
    if ($now > $W1F1Start) {
       if ( in_array($field->id, array(1,13))) { 
          $values['read_only'] = 1;
       }  
       return $values;
    }   
    return $values;
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement