tags:

views:

456

answers:

4

I have a table in MySQL That looks like the following:

date        |storenum   |views 
-------------------------------
08/21/2009  |42         |3  
-------------------------------
08/22/2009  |43         |1    
-------------------------------
08/21/2009  |43         |4  
-------------------------------
08/22/2009  |42         |22
-------------------------------

I know that this is an unconventional date format, but I am using it as opposed to YYYY-MM-DD for ease of use. Yet i have to search this database to get all "View Records" Between two dates. In general it works but when I search from a date such as 01/01/2009 to 01/01/2010 I get no results.

Does anyone have any suggestions? Constructive criticism is welcome. Thanks!

+3  A: 

In all honesty, change the way you store dates. But for working with what you have now, you could look into using STR_TO_DATE() to convert these to dates that MySQL can work with.

Jonathan Sampson
Jon, should i change it to YYYY-MM-DD, and format them before being sent to the user?
Chris B.
There are various ways depending on how you are using it. Some people like using UNIX timestamps which measure the date in seconds from 1970 (Epoch time). Others like to store it in a more human-readable format, like YYYY-MM-DD.
Jonathan Sampson
yyyymmdd or unix time stamp, and yes, format it in the presentation layer (or in the query itself if it is logical to do so).
Itay Moav
Itay, do not modify my answer. Please provide your own.
Jonathan Sampson
Thank you. Also, hello from Marietta, GA! haha.
Chris B.
Hello from Hiram/Dallas, GA :)
Jonathan Sampson
@Jonathan Sampson I was in your opinion until I read the motivation behind SO, Which is not to write an answer per user, but to refine the best answer and refine the question until you get to the optimal result. You may disagree, your right, but this is the reason I can edit Your questions and vice versa (It was not meant to enable me to fix spelling).http://www.joelonsoftware.com/items/2008/09/15.html
Itay Moav
@Italy Moav: Wrong - enter your own answer. If you spend any time here, you'll see that there are multiple answers for a question. Edit was indeed meant to fix spelling and grammar. If your view was correct, there would only be one answer per question. Don't be changing my answers in the future. I don't want you misrepresenting me. If my answer is "wrong", let me figure that out from the down votes.
duffymo
I disagree, sorry.
Itay Moav
I agress with Duffy and Jon.
Chris B.
A: 

Try the search with format "yyyyMMdd" MySql will recognize it for sure.

igors
Even with the way i have the values stored currently?
Chris B.
+2  A: 

You mention ease of use - I think the case you are trying to resolve now is an example of "not so easy to use" :).

I would stick to the default format of dates in MySQL

yyyy-mm-dd h:i:s //php's date formatting

and use DATE fields. This will solve the searching/inserting of dates problem. Though you need to change some code to fix displaying the dates.

bisko
+1  A: 

what is your date field type? for a correct use it should be date or datetime, and seeing what you have it looks like you store you dates in a varchar. am I right?

This also have the advantage you can add indexes, and then search in ranges trough mysql

my suggestion is get whatever the user enter. and then convert it to a mysql date format for searching

something like:

<?php

$mysql_time = date('Y-m-d H:m:s' strotime('05/11/2008'));

$query = sprintf("SELECT * FROM sometable < '%s'", $mysql_time);

?>
Gabriel Sosa