Home > Software design >  Php Disabling past days in datetime_local picker
Php Disabling past days in datetime_local picker

Time:10-09

I am making an event calendar. I am currently looking for ways to disable the previous dates on datetime_local and the minimum date and time is the current. Are there other ways?

<div class="form-group">
<label for="datetime_start" class="control-label">DateTime Start:</label>
<input type="datetime-local" class="form-control" name="datetime_start" id="datetime_start">
</div>

CodePudding user response:

If you look at the definition for datetime-local, you'll see it has a min and max attribute. You can use PHP to set the min time:

<input type="datetime-local" 
     class="form-control" 
     name="datetime_start" 
     id="datetime_start" 
     min="<?php echo date('Y-m-d\TH:i') ?>"
>

Note that this will not always work the way you want

Because of the limited browser support for datetime-local, and the variations in how the inputs work, it may currently still be best to use a framework or library to present these, or to use a custom input of your own. Another option is to use separate date and time inputs, each of which is more widely supported than datetime-local.

  • Related