Aero77 Posted May 11, 2014 Share Posted May 11, 2014 I'm having a fuel score list and I would like users to choose a month and they can see the average fuel consumption that month. Right now I have a working code that shows the average for the last 30 days, but I have added a drop down menu with all the months inside. How can I change the query depending on the month they choose ? If they choose April the value would be 4, but I dont know how to pick only records from April when the dates in the database are like 2014-04-15 format. How can I do this ? I will also have to add a year selector box of course. Hope anyone can help. This is my current code $dx = date('Y-m-d', strtotime('today - 30 days')); $query = "SELECT m.username, m.id, 10 * SUM(d.liter) / (MAX(d.km) - MIN(d.km)) as AvgFuel FROM members AS m LEFT OUTER JOIN diesel AS d ON d.userid = m.id AND d.dato >= '$dx' WHERE d.liter > 0 GROUP BY m.id ORDER BY AvgFuel ASC"; $sql=$oDB->prepare($query); $sql->execute(); $row = $sql->fetchAll(); return $row; Quote Link to comment https://forums.phpfreaks.com/topic/288412-records-from-a-month/ Share on other sites More sharing options...
Barand Posted May 11, 2014 Share Posted May 11, 2014 ... WHERE YEAR(dbdate) = $year AND MONTH(dbdate) = $month Quote Link to comment https://forums.phpfreaks.com/topic/288412-records-from-a-month/#findComment-1479089 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.