how to block past dates in php and in input?

3.4k views Asked by At
<input id="datepicker" name="datepicker" class="calendar" />

Here is my script:

 <script>

    $(function() {
      $( "#datepicker" ).datepicker({ minDate: 0});
    });
    </script>

here php

if( $date < strtotime( date('m/d/Y', $time )) ) {
     echo "<script>alert('You can't put past day..'); </script>"; }

I want to block the users enter past date wrongly or as know..but it does not work..i cant block..how can i block users enter past dates? i want only users can enter today n next days..so i want to filter with input in php.

2

There are 2 answers

5
Barmar On

strtotime() doesn't take a format string, the function you want to use is DateTime::createFromFormat. It returns a DateTime object, not a timestamp, so you need to compare it with a DateTime:

$date = $_POST['datepicker'];
if (DateTime::createFromFormat('m/d/Y', $date) < new DateTime()) {
    echo "<script>alert('You can't put past day..'); </script>";
} else {
    // code to insert into database
}

DEMO

5
Umut Bulut On

i fixed n maybe it help any one who need like me..

<input id="datepicker" name="datepicker" class="calendar" /><br />


  <script>
$(function() {
  $( "#datepicker" ).datepicker({ minDate: 0});
});
</script>

$year=$_POST['year'];
$date=$_POST['datepicker'];
$timer=$_POST['timer'];

$time=time();


        $start_data = date('m/d/Y', time() - (86400));

    $now=date('m/d/Y', $time);


if(  $date  < $start_data ){
    echo "<script>alert('Failed..'); </script>";

    }

thanks for ur help again..