-1

When we declare the java script the validation not applied and not fired an error wile the is same as input type i have declared the code in php file.

I have used Xammp server, running MySQL 5, PHP 7.6.2 and Apache 2.

<script  type="text/javascript">
    //Validation for Stratdate & Enddate for New Ticket creation form                       

    $("#tedate").change(function () {

        var objFromDate = document.getElementById("tsdate").value; 
        var objToDate = document.getElementById("tedate").value;

        var FromDate = new Date(objFromDate);
        var ToDate = new Date(objToDate);

        if(FromDate > ToDate )
        {
            alert("Due Date Should Be Greater Than Start Date");
            document.getElementById("tedate").value = "";
            return false; 
        }

    });
</script>

start date

<input type='date' id="tsdate" class="form-control col-md-6"  placeholder="mm-dd-yyyy" name="startdate">

due date

<input type='date' id="tedate" class="form-control col-md-6" placeholder="Enter Due Date" name="enddate" required />

I want validate due date is greater than start date

Repeat Spacer
  • 373
  • 3
  • 17

2 Answers2

2

Use strtotime function in php


if(strtotime($date1) < strtotime($date2)){
      /* Code */
}


SachinPatil4991
  • 774
  • 6
  • 13
0

Use getTime() function which will convert the time into epoch format.For more details visit https://www.epochconverter.com/programming/#javascript

you can do the following to check in the frontend.

Javascript

  $(".data-controller").change(function () {
        var objFromDate = document.getElementById("tsdate").value;
        var objToDate = document.getElementById("tedate").value;

        var FromDate = (new Date(objFromDate).getTime()) / 1000;
        var ToDate = (new Date(objToDate).getTime()) / 1000;

        if (FromDate > ToDate) {
            alert("Due Date Should Be Greater Than Start Date");
        }

    });

HTML

<input type='date' id="tsdate" class="form-control col-md-6 data-controller" placeholder="mm-dd-yyyy"
               name="startdate">  // class data-controller added
<input type='date' id="tedate" class="form-control col-md-6 data-controller" placeholder="Enter Due Date"
               name="enddate"
               required/> // class data-controller added
Sundar Ban
  • 589
  • 5
  • 16