catsgirl008
catsgirl008

Reputation: 691

Help implementing past date validation

I need to implement something that checks if a given date is greater than today. If for example, I input a date of April 19, 2011 while today is April 15, 2011, there should be some validator/pop up error. How do I implement this?

I have my system date (today's date) working fine through php. I just don't know how to create a validation/error message when the user inputs a higher date than today.

Upvotes: 3

Views: 893

Answers (2)

Wh1T3h4Ck5
Wh1T3h4Ck5

Reputation: 8509

For example (in PHP)

date_default_timezone_set(date_default_timezone_get()); // not necessary here

$today = strtotime('2011-04-15');
$users_day = strtotime('2011-04-19');

if ($users_day > $today) {
  echo "Error";
  }
else {
  echo "OK";
  }

Example above outputs

Error

...because April 19, 2011 (user's input) is greater than April 15th, 2011 (today).

Upvotes: 0

Scherbius.com
Scherbius.com

Reputation: 3414

It can be done with PHP (on server side) and with JavaScript (on client side, in browser).

Here is an example of how to do it on JavaScript:

var currentTime = new Date()
    month = currentTime.getMonth(),
    day = currentTime.getDate(),
    year = currentTime.getFullYear(),
    today = year + "-" + month + "-" + day;

var users_day = '2011-04-19';

if (users_day > today) {
     alert ("Entered day is greater than today");
}
 else {
     alert ("Today is greater than entered day");
 }

Upvotes: 1

Related Questions