0

How should I disable past dates in the datepicker only using HTML?

Example: Suppose today it's 2000/12/21, this is how the datepicker should look.

enter image description here

1 Answer 1

2

HTML5 supports the min and max attribute.

Play around with the example below and you'll understand how it works!

  Enter a date before 2000-12-21:
  <input type="date" name="example1" max="2000-12-20"><br>

  Enter a date after 2000-12-21:
  <input type="date" name="example2" min="2000-12-22"><br>

Just use PHP to automatically update the datepicker to the current date:

<input type="date" name="example3" min="<?= date('Y-m-d') ?>">

NOTE: Internet Explorer 9 (and lower) doesn't support this feature.

Sign up to request clarification or add additional context in comments.

1 Comment

It's much better to use a templating system like Twig and avoid mixing PHP and HTML, in which case the last code would become <input type="date" name="example3" min="{{ 'now'|date('Y-m-d') }}">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.