DiChrist DiChrist - 1 year ago 45
PHP Question

How to set the max and min value for year in html

I need to verify the max and min value from a date before submit a form. I want to send dates between 01-01-2005 and 12-12-2020. How can I do ti in php. If it's not possible in php, can be in javascript (jquery)

<input type="date" name="datepost" value="<?php echo date('Y-m-d');"/>

Answer Source

Like I mentioned in the comments, all you really need to limit the range of inputs in a date-input element is the min and max attributes. No JavaScript needed!

<input type="date" name="datepost" min="2005-01-01" max="2020-12-12" value="<?php echo date('Y-m-d');"/>

This will limit the date, allowing January 1st, 2005 to be the minimum date to be available for selection in the form, and December 12th, 2020 to be the maximum date available.

Although, the user can specify the date by simply typing in a date of their choice (instead of selecting one with their cursor from the selection-window), outside of those parameters, so you should validate this in PHP (on a server-side level) too, which you should do in any case for all user-inputs. You can also validate it in JavaScript, but always validate server-side. See the other answer for a way of validating it in PHP.