2

I have a php page which allows a user to sort pieces of information by several factors. A new requirement is to sort by "all items which have been registered in the last 15 days". I store my dates in the MYSQL table as mm/dd/yyyy.

The information is passed and picked up on the same page using the $_GET variable but I am unable for some reason to get the code to work. I have looked on numerous website but am unable to find a solution that works.

Ultimately, the script would work as follows:

select all persons who's KDATE is within 15 days of today's date (e.g., if today is 8/19/2010, everybody who registred from 8/04/2010 and on would appear).

My script so far (which does not work) is:

if (isset($_GET['date'])) {
     $query = "SELECT * 
                 FROM persons 
                WHERE DATE_SUB(CURDATE(),INTERVAL 15 DAY) <= KDATE 
             ORDER BY KDATE ASC";
}

Update 1:

KDATE IS TEXT - i apologize but the KDATE is stored as TEXT

Update 2:

The answer provided by Colin solved my issue. I will look into trying to convert the data into datetime format but am hoping the group can provide realistic benefits of doing so.

Thank you all again

12
  • You mean the KDATE field is a VARCHAR? Commented Aug 19, 2010 at 20:54
  • 2
    Store your dates using the MySQL Date (or DateTime) type and you won't have these issues ;-)... Commented Aug 19, 2010 at 20:56
  • @ircmaxell: That's so crazy, it might just work =) Commented Aug 19, 2010 at 20:57
  • 1
    @JM4: There's a difference between overkill and just plain wasteful/inefficient. It would be like bringing a nuclear missile to a knife fight. Sure, it's more powerful, but it's also a lot harder to move around with (and hence slows you down a lot)... And it's completely wasting the resources at hand... Commented Aug 20, 2010 at 12:27
  • 1
    Oh and @OMG: The TEXT data type supports up to 65,536 characters (I say up to, since it depends on the characters being stored). And it's not just overkill, it's inefficient since the data isn't even stored with the row (so doing selects on it will thrash the drives with seeks since it can't just use the row buffer). Only use TEXT if you know you need more than 255 characters. Otherwise use either CHAR or VARCHAR (Which store their data in the row itself)... Commented Aug 20, 2010 at 12:46

2 Answers 2

3

First of all, it's a really bad idea to use VARCHAR instead of DATE if you want a collumn with dates only.

If you want to use a string as a date, you'll need to convert it with STR_TO_DATE() and you might wan't to use those instructions to correctly format your date.

This should do it:

SELECT * 
FROM persons 
WHERE DATE_SUB(CURDATE(),INTERVAL 15 DAY) <= STR_TO_DATE(KDATE, "%c/%d/%Y")
ORDER BY STR_TO_DATE(KDATE, "%c/%d/%Y") ASC
Sign up to request clarification or add additional context in comments.

2 Comments

this only orders the data, it does not return any results relatively to my main query
Be advised that this style query will not be able to use indexes. Which means it will require a full table scan for every single query. If you have lots of rows, or lots of these queries it will bring your server to its knees VERY quickly... This will work to solve your problem, but the better solution would be to change KDATE to a DATE column data type...
2

Because kdate is VARCHAR, you need to use STR_TO_DATE to change it to a DATETIME.

You need to fix kdate data that does not fit that pattern (mm/dd/yyyy) before running this:

  SELECT * 
    FROM persons 
   WHERE DATE_SUB(CURDATE(),INTERVAL 15 DAY) <= STR_TO_DATE(KDATE, 'm/%d/%Y')
ORDER BY STR_TO_DATE(KDATE, 'm/%d/%Y') ASC
  • This means that an index on kdate is useless, because of having to change the data type.

Once it's a DATETIME, you can use DATE_FORMAT to change the format as you like.

2 Comments

so if I understand correctly - you are saying that my overall purpose is impossible as long as my dates are in VARCHAR format?
@JM4: Not impossible. Try the query, but it's very possible you will get an error because at least one kdate value doesn't match the date format. So you need to fix those before the query will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.