Skip to content
Advertisement

Php Disabling past days in datetime_local picker

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>

Advertisement

Answer

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-dTH: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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement