开发者

Help implementing past date validation

I need to imp开发者_StackOverflow中文版lement 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.


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");
 }


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜