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

Time:08-24

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 >
    <label>Join Date</label>
    <input type="date" name="joindate"  value="<?php echo $joindate ?>">
    <span ><?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;
}

CodePudding user response:

<?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";
    }
  • Related