-1

I need to create a code in c# to check either the date picked is tomorrow onwards or has passed by today.

My idea is $checkDateInPass will get the current date minus with 1 day. Thus when a user enter a date either it was on THAT date or afterwards, $checkPass will fly a green flag to say that it passed the first validation. If it was otherwise, it will turned into an error.

Here's the code for date validation:

$checkDateInPass = date('d/m/Y',strtotime("-1 days"));
if($checkInDate > $checkDateInPass)
{
    $checkPass += 1;
}
else
{
    $message = "Please pick a proper date to check in.";
    echo "<script type='text/javascript'>alert('$message');</script>";
}

Based on the above written code it work just fine IF I picked the check in date in the same month, but if I picked the check in date on the next month other than the current month, it doesn't work as expected.

Here's an example, lets say that $checkDateInPass is 27/12/2016:

A: I picked 29/12/2016 for first date, and 04/01/2017 for second date. It worked just fine.

B: I picked 04/01/2017 for first date, and 07/01/2017 for second date. It turns out to be wrong.

Is there any other IF statement to be considered for this statement?

Asyraf
  • 35
  • 1
  • 8

1 Answers1

0

It looks like you're comparing strings containing formatted time, not actual times. You can compare the time this way:

$checkDateInPass = date('d/m/Y',strtotime("-1 days"));
if(strtotime($checkInDate) > strtotime($checkDateInPass))
{
    $checkPass += 1;
}
else
{
    $message = "Please pick a proper date to check in.";
    echo "<script type='text/javascript'>alert('$message');</script>";
}
Erik A
  • 31,639
  • 12
  • 42
  • 67
  • this can be fulfilled both the conditions. – Soni Vimalkumar Dec 28 '16 at 12:29
  • oh.. so.. i still need to use strtotime when making the comparison because it is strings without it? I thought it was still an actual times though.. my mistake..sigh.. thank you sir, u're a life saver. – Asyraf Dec 28 '16 at 12:34