Solved

Comparing two dates problem

Posted on 2007-04-09
9
257 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:RealSnaD
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 24

Expert Comment

by:glcummins
ID: 18876828
Can you show us the code you are using to make the comparison?
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 18876963
You can create a unix timestamp and compare those.

-r-

0
 
LVL 7

Author Comment

by:RealSnaD
ID: 18877006
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");
       }
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 50

Assisted Solution

by:Steve Bink
Steve Bink earned 50 total points
ID: 18877028
You're going through a lot of trouble to reinvent a particular wheel.  Roonaan has already posted my strategy.  Change the db value to unix timestamp, build another unix timestamp from your post/get data, then compare.  Look here:

http://www.php.net/manual/en/function.mktime.php
http://www.php.net/manual/en/function.strtotime.php

0
 
LVL 7

Author Comment

by:RealSnaD
ID: 18877089
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.
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 450 total points
ID: 18877102
24:00 / 24:59 need just be converted to 0:00-0:59 on the next day.

You can get the current time using time() of mktime() or gmtime().
-r-
0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 18877438
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.
0
 
LVL 7

Author Comment

by:RealSnaD
ID: 18877680
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.
0
 
LVL 7

Author Comment

by:RealSnaD
ID: 18883129
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.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now