I'm sure my issue is simple to resolve, but I've had no luck. I want to retrieve a set of information from a database for rows that were submitted between two dates. These two dates will be provided by the user in a form and saved in the variables $start and $end.
You need to put single quotes around the two date variables in the query. (They are strings, not numbers.)
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Have you echoed the POST data after you convert them with the strtotime() to see what they are, then compare them to the ones in the database manually ?
@NogDog; I've tried putting single quotes around $startdate and $enddate and that doesn't work.
@SyncUpSolutions; I've echoed out the dates elsewhere so I can check the range and they show up as I expect them to, with the entered strings converted into dates, e.g. "tomorrow" becomes "08-12-2010".
Is submitted a date, datetime, or timestamp type of column?
Also, you might want to consider using the BETWEEN...AND operators.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
The database column submitted is set to store data as TIMESTAMP.
I was originally using BETWEEN and AND for my query, but they didn't work for me either, so I swapped them for the greater than and less than operators so that I as I edited the two separate statements the would be completely independent of each other and I could clearly see if it made any difference.
It previously looked like this:
PHP Code:
$list = mysql_query("SELECT id FROM collection WHERE (category= 'subject') AND (submitted BETWEEN $startdate AND $enddate)");
My problem seems to be that the script is reading the value in the database as later than both my submitted dates even when it isn't. If I remove one of the entered values completely, all of the db information where category= 'subject' shows up, regardless of dates.
Maybe try forcing the values to be treated as dates?
PHP Code:
"...DATE(submitted) BETWEEN '$startdate' AND '$enddate'"
or...
PHP Code:
"...DATE(submitted) BETWEEN STR_TO_DATE('$startdate', '%Y-%m-%d') AND STR_TO_DATE('$enddate', '%Y-%m-%d')"
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Afraid I'm stumped. Seems like it should work, unless there's a silly logic error we're not seeing.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Can you provide the schema and a few sample rows? I'm curious that if by replicating the problem with a much smaller dataset the answer will be easier to spot.
Bookmarks