Link to home
Start Free TrialLog in
Avatar of Yury Merezhkov
Yury MerezhkovFlag for United States of America

asked on

Comparing two dates problem

Hi guys,

I have a field DATETIME in the database. I'm comparing values in that field to current date presented in the same format. When the current date is larger than the one in the database the error is supposed to pop up saying that the date is in the past. Everything works fine unless the database value is 2008-01-10 24:00:00. Or 24:01:00 - 24:59:00. In this case, it still says that the current date is larger than the database one but it's not. What can be the problem?

Thanks.
Avatar of glcummins
glcummins
Flag of United States of America image

Can you show us the code you are using to make the comparison?
Avatar of Roonaan
You can create a unix timestamp and compare those.

-r-

Avatar of Yury Merezhkov

ASKER

Code as follows:

      $day              = $_REQUEST["eventday"];
      $month        = $_REQUEST["eventmonth"];
      $year        = $_REQUEST["eventyear"];
      $hours        = $_REQUEST["eventhours"];
      $minutes        = $_REQUEST["eventminutes"];
      $ampm        = $_REQUEST["eventampm"];

        // Adjusting Entered Date
      if(strlen(trim($month)) == 1) {
            $month = "0".$month;
      }
      if(strlen(trim($day)) == 1) {
            $day = "0".$day;
      }
      if(strlen(trim($hours)) == 1) {
            $hours = "0".$hours;
      }
      if((trim($ampm) == "PM") && ($hours != 12)) {
            $hours += 12;
      } else if((trim($ampm) == "AM") && ($hours == 12)) {
            $hours += 12;
      }
      $eventDate    = $year."-".$month."-".$day." ".$hours.":".$minutes.":00";
      $currentDate  = strtotime(date("Y-m-d H:i:s", time()));
      //----------------------------

       if(strtotime($eventDate) <= $currentDate) {
            // Redirect to error page      
                   header("Location:/admin/?o=newevent&id=".$eventId."&r=e3");
       }
SOLUTION
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What if I don't have the database at all? Yes, forget about the database. I get the date from user and I need to compare it to the current date. If the current date is larger I give an error saying the date you entered is in the past. My code works fine unless user enters 12:00 - 12:59 AM which is 24:00 - 24:59.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
On the next day or the same day?  In any case, I still agree with Roonaan.  

If you haven't already, change your form to use <select> elements.  This would also allow for some simple javascript form validation...you can verify the date's validity before the user submits your form.
I don't like JS solutions in this case since user can have JS turned off and then it will cause other errors to show up. I'll try Roonaan's solution a bit later and let you guys know. Thanks.
All I had to do is to change this

else if((trim($ampm) == "AM") && ($hours == 12)) {
            $hours += 12;
}

to

else if((trim($ampm) == "AM") && ($hours == 12)) {
            $hours -= 12;
}

because it's supposed to be 00 not 24. Now it works fine and comparison is done correctly now. But thanks to u guys anyways.