There is an input field, the user enters a date into it. I need the user's date to be not less than (+2 days from today) and not more than (+ half a year from today) .
Below is the date verification code in javascript .
Task : you need to do the same check, only in php .
(Date display should be in format 12/31/2016)
var tomorrow = new Date(); // получаем текущую дату №1 var pieceyear = new Date(); // получаем текущую дату №2 function dateFormat(date) { return ("0" + (date.getDate())).slice(-2) + '.' + ("0" + (date.getMonth() + 1)).slice(-2) + '.' + date.getFullYear(); } function parseDate(dateStr) { var parts = dateStr.split('.'); return new Date(+parts[2], -1 + (+parts[1]), +parts[0]); } tomorrow.setDate(tomorrow.getDate() + 2); // к дате №1 добавляем 2 дня pieceyear.setMonth(pieceyear.getMonth() + 6); // к дате №2 добавляем 6 месяцев $('#datepicker').val(dateFormat(tomorrow)); // значение поля становится на 2 дня больше сегодняшнего дня function datevalidate() { var value = parseDate($('#datepicker').val()); if (value < tomorrow) { // если дата меньше чем послезавтра $('#datepicker').val(dateFormat(tomorrow)); // присваиваем дату послезавтра } else if (value > pieceyear) { // если дата больше чем + пол года $('#datepicker').val(dateFormat(pieceyear)); // присваиваем дату на пол года больше } } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="datepicker" onchange="datevalidate()">