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! Link to comment https://forums.phpfreaks.com/topic/178616-identify-a-month-from-a-date/ 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 Link to comment https://forums.phpfreaks.com/topic/178616-identify-a-month-from-a-date/#findComment-942072 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? Link to comment https://forums.phpfreaks.com/topic/178616-identify-a-month-from-a-date/#findComment-942074 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 Link to comment https://forums.phpfreaks.com/topic/178616-identify-a-month-from-a-date/#findComment-942082 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; Link to comment https://forums.phpfreaks.com/topic/178616-identify-a-month-from-a-date/#findComment-942086 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"? Link to comment https://forums.phpfreaks.com/topic/178616-identify-a-month-from-a-date/#findComment-942090 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); Link to comment https://forums.phpfreaks.com/topic/178616-identify-a-month-from-a-date/#findComment-942093 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); Link to comment https://forums.phpfreaks.com/topic/178616-identify-a-month-from-a-date/#findComment-942103 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.