Skip to content
Advertisement

Validate Date input not empty and not greater than todays date in Php

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";
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement