Reputation: 65
I Want to compare only month and year in SQL Using PHP, but when I only use month and year in the SQL query the only result is 00-00-0000.
$mounth = date("Y-m", time());
$histmessql = "SELECT * FROM historico WHERE data = $mounth";
Upvotes: 0
Views: 89
Reputation: 751
I assume that the data
column in your database is any of the date/datetime. With that assumption, I think you can make use of the DATE_FORMAT(date,format)
function and your code could look like
$mounth = date("Y-m", time());
$histmessql = "SELECT * FROM historico WHERE DATE_FORMAT(data, '%Y-%m') = '$mounth'";
You can read more https://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_date-format
Upvotes: 0
Reputation: 3754
I think you have to update your sql query to :
$histmessql = "SELECT * FROM historico WHERE data like '%$mounth%'";
using = , this fetch a completed equality , while your have to use like
, here is the examples :
WHERE CustomerName LIKE 'a%'
Finds any values that start with "a"
WHERE CustomerName LIKE '%a'
Finds any values that end with "a"
WHERE CustomerName LIKE '%or%'
Finds any values that have "or" in any
position
WHERE CustomerName LIKE '_r%'
Finds any values that have "r" in the
second position
WHERE CustomerName LIKE 'a_%_%'
Finds any values that start with "a"
and are at least 3 characters in length
WHERE ContactName LIKE 'a%o'
Finds any values that start with "a" and
ends with "o"
Upvotes: 1