tejama Posted March 13, 2007 Share Posted March 13, 2007 Hi everyone, I'm trying to extract data in a certain range of date (say 1 month's worth), but my results aren't what I'm expecting. Can you guys have a look at my code and suggest what may not be coded properly? function display_archive($month) { $tempdate = $month. " 1, 2007"; $startdate = date("F d, Y", strtotime($tempdate)); $tempdate2 = $month. " 30, 2007"; $enddate = date("F d, Y", strtotime($tempdate2)); db_connect(); $result = mysql_query("select * from blog where entered BETWEEN '$date' and '$enddate'"); $num_rows = mysql_num_rows($result); } Link to comment https://forums.phpfreaks.com/topic/42523-solved-date-range-in-mysql-query/ Share on other sites More sharing options...
AV1611 Posted March 13, 2007 Share Posted March 13, 2007 I might be off on this, but I wrote a similar script a while back and here are some snippets from it. Keep in mind this was talking to ODBC (VFP) and not MySQL so syntax might be wrong but the method will work: $today="{d '".date('Y-m-d')."'}"; $ninetyDay="{d '".date('Y-m-d', strtotime('-120 days'))."'}"; $connect = odbc_connect("xxxx", "xxxx", "xxxx"); $query = "SELECT itdatshta.`it_date`, itdatshta.`insptype`, itdatshta.`process`, itdatshta.`product`, itdatshta.`descr`, itdatshta.`rev_level`, itdatshta.`serialno`, itdatshta.`failed` FROM `itdatshta` itdatshta WHERE itdatshta.`it_date` >= $ninetyDay AND itdatshta.`it_date` <= $today AND process = 'Functional Test'"; Link to comment https://forums.phpfreaks.com/topic/42523-solved-date-range-in-mysql-query/#findComment-206322 Share on other sites More sharing options...
skali Posted March 13, 2007 Share Posted March 13, 2007 function display_archive($month) { $tempdate = $month. " 1, 2007"; $startdate = date("Y-m-d", strtotime($tempdate)); $tempdate2 = $month. " 30, 2007"; $enddate = date("Y-m-d", strtotime($tempdate2)); db_connect(); $result = mysql_query("select * from blog where entered BETWEEN '$date' and '$enddate'"); $num_rows = mysql_num_rows($result); } Link to comment https://forums.phpfreaks.com/topic/42523-solved-date-range-in-mysql-query/#findComment-206329 Share on other sites More sharing options...
tejama Posted March 13, 2007 Author Share Posted March 13, 2007 I actually figured it out, here is the code I used: function archive($month) { $tempdate = $month. " 1, 2007"; $startdate = date("Y-m-d H:i:s", strtotime($tempdate)); $tempdate2 = $month. " 11, 2007"; $enddate = date("Y-m-d H:i:s", strtotime($tempdate2)); db_connect(); $result = mysql_query("select * from blog where entered >= '$startdate' and entered <= '$enddate'"); $num_rows = mysql_num_rows($result); } Thanks for the help though! Link to comment https://forums.phpfreaks.com/topic/42523-solved-date-range-in-mysql-query/#findComment-206331 Share on other sites More sharing options...
tejama Posted March 13, 2007 Author Share Posted March 13, 2007 Thanks Skali...got it just before I read your post! Link to comment https://forums.phpfreaks.com/topic/42523-solved-date-range-in-mysql-query/#findComment-206332 Share on other sites More sharing options...
suttercain Posted March 13, 2007 Share Posted March 13, 2007 Is >= '$startdate' and entered <= what fixed it? Link to comment https://forums.phpfreaks.com/topic/42523-solved-date-range-in-mysql-query/#findComment-206397 Share on other sites More sharing options...
tejama Posted March 13, 2007 Author Share Posted March 13, 2007 Nope, what fixed it was changing the date declaration from: $startdate = date("F d, Y", strtotime($tempdate)); to: $startdate = date("Y-m-d H:i:s", strtotime($tempdate)); for both $startdate and $enddate. Link to comment https://forums.phpfreaks.com/topic/42523-solved-date-range-in-mysql-query/#findComment-206419 Share on other sites More sharing options...
suttercain Posted March 13, 2007 Share Posted March 13, 2007 Ahh... Thanks for the heads up. I am learning so I always look at the TOPIC SOLVED threads to run through it. Link to comment https://forums.phpfreaks.com/topic/42523-solved-date-range-in-mysql-query/#findComment-206421 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.