Help please, am trying to validate a form date input not empty and should not be greater that today’s date. this is what I did so far. am getting 000-00-00 inserted in MySQL db. what am I doing wrong? here is what in the form input
<div class="form-group"> <label>Join Date</label> <input type="date" name="joindate" class="form-control <?php echo (!empty($joindate_err)) ? 'is-invalid' : ''; ?> " value="<?php echo $joindate ?>"> <span class="invalid-feedback"><?php echo $joindate_err; ?></span> </div>
the php tag above has this validations
//date validation $input_joindate = trim($_POST["joindate"]); if (empty($input_joindate)) { $joindate_err = "Select join date"; } if (!empty($input_joindate)) { $input_joindate = date('Y-m-d', strtotime($input_joindate)); $today = strtotime("now"); if (($input_joindate) > $today) $joindate_err = "Date should not be in the future"; } else { $joindate = $input_joindate; }
Advertisement
Answer
<?php $today = date("Y-m-d"); $joindate = $_POST["joindate"]; if (empty($joindate) || !isset($joindate)) { $joindate_err = "Select join date"; } elseif (strtotime($today) < strtotime($joindate)) { $joindate_err = "Date should not be in the future"; }