0

In HTML, i'm entering a date (mm/dd/yyyy) using the new HTML 5 date picker:

 <input name="date" type="date">

I want to validate if the date entered is older than today's date

$this_date = $_POST['date'];

if ($this_date is older the today's date) {
    echo 'The date entered must be greater than today's date';
} else {
    echo 'The date is valid!';
}

How do I go about?

Thanks

Marco
  • 2,687
  • 7
  • 45
  • 61
  • possible duplicate of [PHP - How can I check if the current date/time is past a set date/time?](http://stackoverflow.com/questions/2832467/php-how-can-i-check-if-the-current-date-time-is-past-a-set-date-time) – brrystrw Mar 17 '14 at 20:27

3 Answers3

1
if (time() > strtotime($_POST['date'])) {
    echo "The date entered must be greater than today's date";
} else {
    echo "The date is valid!";
}
brrystrw
  • 471
  • 7
  • 20
1
<?php
   $this_date = $_POST['date'];

   if (strtotime($this_date . " 00:00:00") > strtotime(date("m/d/Y 00:00:00"))) {
       echo "The date entered must be greater than today's date";
   } else {
       echo "The date is valid!";
    }
?>

Also, you should check the input from the user for validity using preg_match() or some other techniques.

Be aware that the PHP will handle invalid dates like 02/30/2014. It will evaluate to 03/02/2014.

Gábor Héja
  • 458
  • 7
  • 17
1

Here you have the right answer:

$diff = strtotime($_POST['date'])>0? strtotime('Today')-strtotime($_POST['date']):NULL;
$res = $diff>0?"The date is valid!":"The date entered must be greater than today's date";
echo $res;
4EACH
  • 2,132
  • 4
  • 20
  • 28