nbarone Posted July 14, 2009 Share Posted July 14, 2009 I have some data I want to query between dates. In the MySql database, they have a datetime field (Y-m-d h:i:s) How can I effectivily query a range of date like: SELECT * FROM table WHERE tb_date < "2009-01-01" AND tb_date > "2009-06-01" Quote Link to comment https://forums.phpfreaks.com/topic/165968-solved-select-data-from-a-date-range/ Share on other sites More sharing options...
J.Daniels Posted July 14, 2009 Share Posted July 14, 2009 Have you tested that statement? The syntax looks correct, however, your conditionals would return anything outside of the date range you specified. Quote Link to comment https://forums.phpfreaks.com/topic/165968-solved-select-data-from-a-date-range/#findComment-875364 Share on other sites More sharing options...
rhodesa Posted July 14, 2009 Share Posted July 14, 2009 yeah...you just have to switch the > and <: SELECT * FROM table WHERE tb_date > '2009-01-01' AND tb_date < '2009-06-01' or use a BETWEEN: SELECT * FROM table WHERE tb_date BETWEEN '2009-01-01' AND '2009-06-01' Quote Link to comment https://forums.phpfreaks.com/topic/165968-solved-select-data-from-a-date-range/#findComment-875366 Share on other sites More sharing options...
nbarone Posted July 14, 2009 Author Share Posted July 14, 2009 here's what I got: <?php function getNumRows($qry){ if($qry <> NULL){ $qry = "SELECT * FROM epostcard WHERE ".$qry; } else { $qry = "SELECT * FROM epostcard"; } $result = mysql_query($qry); $numRows = mysql_num_rows($result); unset($qry,$result); return $numRows; } $totalPostcards = getNumRows("p_pcSentDate <= '". $dateStart . "' AND p_pcSentDate >= '" . $dateEnd . "'"); this returns 0 rows. (incorrect) $totalPostcards = getNumRows("p_pcSentDate BETWEEN '". $dateStart . "' AND '" . $dateEnd . "'"); this returns the correct rows, however I need to get the rows that are on dateStart and dateEnd - this does the rows BETWEEN (obv), Quote Link to comment https://forums.phpfreaks.com/topic/165968-solved-select-data-from-a-date-range/#findComment-875387 Share on other sites More sharing options...
Daniel0 Posted July 14, 2009 Share Posted July 14, 2009 Switch your end and start dates. You're selecting the rows outside that date range. Quote Link to comment https://forums.phpfreaks.com/topic/165968-solved-select-data-from-a-date-range/#findComment-875390 Share on other sites More sharing options...
rhodesa Posted July 14, 2009 Share Posted July 14, 2009 You are still doing <= START and >= END. The date can't be both before a start date and after an end date. you want after a start date and before and end date: $totalPostcards = getNumRows("p_pcSentDate >= '". $dateStart . "' AND p_pcSentDate <= '" . $dateEnd . "'"); Quote Link to comment https://forums.phpfreaks.com/topic/165968-solved-select-data-from-a-date-range/#findComment-875391 Share on other sites More sharing options...
nbarone Posted July 14, 2009 Author Share Posted July 14, 2009 I give you both the right to eSlap me. it's been a long day.... thank you. Quote Link to comment https://forums.phpfreaks.com/topic/165968-solved-select-data-from-a-date-range/#findComment-875394 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.