jeff5656 Posted October 22, 2009 Share Posted October 22, 2009 I have a date field and I want to do this query: $query = "SELECT * FROM staffsched WHERE service = '$service' ORDER BY calldate "; But how do I only select records that match a certain month? For example if a record has calldate of 2009-10-10 It would be selected if I only wanted records from october. Thanks! Quote Link to comment Share on other sites More sharing options...
cbolson Posted October 22, 2009 Share Posted October 22, 2009 $month=10; $query = "SELECT * FROM staffsched WHERE service = '$service' AND MONTH(calldate)=$month ORDER BY calldate "; However, you probably want a specific year as well; $month=10; $year=2009; $query = "SELECT * FROM staffsched WHERE service = '$service' AND MONTH(calldate)=$month AND YEAR(calldate)=$year ORDER BY calldate "; Chris Quote Link to comment Share on other sites More sharing options...
jeff5656 Posted October 22, 2009 Author Share Posted October 22, 2009 Thanks. Would this work for defining the current month: $month = date('m'); or does it need to be a number in the above query? Quote Link to comment Share on other sites More sharing options...
cbolson Posted October 22, 2009 Share Posted October 22, 2009 date('m'); returns a number (01-12) so that should work. Chris Quote Link to comment Share on other sites More sharing options...
dreamlove Posted October 22, 2009 Share Posted October 22, 2009 $month=10; $query = "SELECT * FROM staffsched WHERE service = '$service' AND MONTH(calldate)=$month ORDER BY calldate "; However, you probably want a specific year as well; $month=10; $year=2009; $query = "SELECT * FROM staffsched WHERE service = '$service' AND MONTH(calldate)=$month AND YEAR(calldate)=$year ORDER BY calldate "; Chris in the case of both year and month: $query = "select * from staffsched where service='$service' AND LEFT(calldate,7)=$year_month ORDER BY calldate; Quote Link to comment Share on other sites More sharing options...
jeff5656 Posted October 22, 2009 Author Share Posted October 22, 2009 thanks I am learning more about dates Ok lets say I select a record $row['calldate']; how do I echo only the day? For exmpke if alldate is october 12, 2009, how do I echo $row['calldate'] to only display "12"? Quote Link to comment Share on other sites More sharing options...
dreamlove Posted October 22, 2009 Share Posted October 22, 2009 thanks I am learning more about dates Ok lets say I select a record $row['calldate']; how do I echo only the day? For exmpke if alldate is october 12, 2009, how do I echo $row['calldate'] to only display "12"? substr($calldate,5,2); Quote Link to comment Share on other sites More sharing options...
jeff5656 Posted October 22, 2009 Author Share Posted October 22, 2009 What does 5 and 2 refer to? Would I do echo substr($row['calldate'],5,2); 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.