开发者

Help implementing past date validation

开发者 https://www.devze.com 2023-02-24 17:23 出处:网络
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

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

精彩评论

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