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? Link to comment https://forums.phpfreaks.com/topic/125408-first-day-ampamp-last-day-of-the-last-6-months/ 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'"); ?> Link to comment https://forums.phpfreaks.com/topic/125408-first-day-ampamp-last-day-of-the-last-6-months/#findComment-648397 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 Link to comment https://forums.phpfreaks.com/topic/125408-first-day-ampamp-last-day-of-the-last-6-months/#findComment-648807 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? Link to comment https://forums.phpfreaks.com/topic/125408-first-day-ampamp-last-day-of-the-last-6-months/#findComment-648811 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 Link to comment https://forums.phpfreaks.com/topic/125408-first-day-ampamp-last-day-of-the-last-6-months/#findComment-648813 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); Link to comment https://forums.phpfreaks.com/topic/125408-first-day-ampamp-last-day-of-the-last-6-months/#findComment-648823 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. Link to comment https://forums.phpfreaks.com/topic/125408-first-day-ampamp-last-day-of-the-last-6-months/#findComment-648830 Share on other sites More sharing options...
F1Fan Posted September 23, 2008 Share Posted September 23, 2008 Did you try the quotes? Link to comment https://forums.phpfreaks.com/topic/125408-first-day-ampamp-last-day-of-the-last-6-months/#findComment-648832 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 Link to comment https://forums.phpfreaks.com/topic/125408-first-day-ampamp-last-day-of-the-last-6-months/#findComment-648840 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); Link to comment https://forums.phpfreaks.com/topic/125408-first-day-ampamp-last-day-of-the-last-6-months/#findComment-648858 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' Link to comment https://forums.phpfreaks.com/topic/125408-first-day-ampamp-last-day-of-the-last-6-months/#findComment-648863 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! Link to comment https://forums.phpfreaks.com/topic/125408-first-day-ampamp-last-day-of-the-last-6-months/#findComment-648876 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! Link to comment https://forums.phpfreaks.com/topic/125408-first-day-ampamp-last-day-of-the-last-6-months/#findComment-648898 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 Link to comment https://forums.phpfreaks.com/topic/125408-first-day-ampamp-last-day-of-the-last-6-months/#findComment-648899 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 Link to comment https://forums.phpfreaks.com/topic/125408-first-day-ampamp-last-day-of-the-last-6-months/#findComment-649037 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.