refiking Posted September 23, 2008 Share Posted September 23, 2008 I need to retrieve records from my db that are timestamped. I need 6 different queries for the last 6 months. I need all the records in a given month. So, how do I code that exactly? Here's what I have so far $time = time(); $lastmonth = strtotime('-1 month'); $sql = mysql_query("SELECT * FROM reports WHERE time < '$time' AND time > '$lastmonth'"); That string won't give me exactly what I'm looking for so, what can I change or do i need to scrap the whole thing and start from scratch? Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 23, 2008 Share Posted September 23, 2008 I'm guessing you want the first second of the month to the last second of the month. Of so, try this: <?php $month = '2008-06-01'; //here's the month you want, as an example $start = strtotime("$month 00:00:00"); $end = strtotime("$month 00:00:00 +1 month"); $sql = mysql_query("SELECT * FROM reports WHERE time BETWEEN '$start' AND '$end'"); ?> Quote Link to comment Share on other sites More sharing options...
refiking Posted September 23, 2008 Author Share Posted September 23, 2008 OK. So, here's the code I have so far: $sepsql = mysql_query("SELECT * FROM records WHERE time BETWEEN '$jstart' AND '$jend'")or die(mysql_error()); $sepnum = mysql_num_rows($sepsql); When I echoed results, here's what I got: $jstart = 09-01-08 00:00:00 $jsend = 10-01-08 00:00:00 $sepnum = 0 $sepnum should have been 6 Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 23, 2008 Share Posted September 23, 2008 That looks OK to me. Is it working? Quote Link to comment Share on other sites More sharing options...
refiking Posted September 23, 2008 Author Share Posted September 23, 2008 The problem is the value for $sepnum should be 6, since there are 6 records that match the query string. But, it returns a value of 0 Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 23, 2008 Share Posted September 23, 2008 Ah, sorry. Well, what format is your "time" column? Also, "time" is a keyword in SQL, so I suggest adding double quotes: $sepsql = mysql_query("SELECT * FROM records WHERE \"time\" BETWEEN '$jstart' AND '$jend'")or die(mysql_error()); $sepnum = mysql_num_rows($sepsql); Quote Link to comment Share on other sites More sharing options...
refiking Posted September 23, 2008 Author Share Posted September 23, 2008 The format is timestamp. I can just change the time field to vtime if that helps. Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 23, 2008 Share Posted September 23, 2008 Did you try the quotes? Quote Link to comment Share on other sites More sharing options...
refiking Posted September 23, 2008 Author Share Posted September 23, 2008 Yeah. I also changed the `time` filed to `vtime` and still nothing Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 23, 2008 Share Posted September 23, 2008 Try echoing your query and running it manually: $query = "SELECT * FROM records WHERE \"vtime\" BETWEEN '$jstart' AND '$jend'"; echo $query; $sepsql = mysql_query($query)or die(mysql_error()); $sepnum = mysql_num_rows($sepsql); Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 23, 2008 Share Posted September 23, 2008 SELECT `column` FROM `table` WHERE MONTH(`vtime`) = '09' AND YEAR(`vtime`) = '2008' Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 23, 2008 Share Posted September 23, 2008 Man, I hate that. You spend WAY too much time on a problem, and someone comes up with some brilliantly simple solution like that. Nice, discomatt! Quote Link to comment Share on other sites More sharing options...
refiking Posted September 23, 2008 Author Share Posted September 23, 2008 Man, I hate that. You spend WAY too much time on a problem, and someone comes up with some brilliantly simple solution like that. Nice, discomatt! Ditto. Thanks for the assistance guys! Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 23, 2008 Share Posted September 23, 2008 To get previous months ( in this example, 2 months ago ) SELECT `your`, `columns`, `here`, @then := DATE_SUB( NOW(), INTERVAL 2 MONTH ) as `prev_date` FROM `your_table` WHERE MONTH( `vtime` ) = MONTH( @then ) AND YEAR( `vtime` ) = YEAR( @then ); Man, I hate that. You spend WAY too much time on a problem, and someone comes up with some brilliantly simple solution like that. Nice, discomatt! Thank you Quote Link to comment Share on other sites More sharing options...
refiking Posted September 23, 2008 Author Share Posted September 23, 2008 How can I search for records on a given week? Say 01/01/08 - 01/08/08 Quote Link to comment 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.