Reputation: 20356
I have a table in a database in a CakePHP application that has a date
field. The datatype of that field is DATETIME. So it shows like this in the table:
2013-08-10 13:34:45
I would like to add a condition in a find() query to filter by year or by month. To filter by year, I can use the LIKE condition to do something like:
$condition = array(
'MyModel.date LIKE' => '%2013%'
);
But how can I filter by months? the approach above will not work because I can have for example something like this as a date:
2013-12-11 11:11:11
If I do LIKE '%11%', my results will be wrong because the month of the previous date is actually November (or december if you live in Canada). So, how can I add a condition on the date field of my table to filter by the correct month?
Thanks in advance for any help
Upvotes: 1
Views: 6016
Reputation: 46900
WHERE MONTH(date)=6 // June
You can also use the YEAR
function
WHERE YEAR(date) =2013
Upvotes: 5