Tag Archives: Day

Limit CakePHP2 Results by YEAR, MONTH or DAY

You can never have to many tricks up your sleeves when it comes to pulling the data you need from your database. Sometimes you only want to return a limited set of results based on the year, month or day. So how do you do this?

The solution is quite simple. The following code can be used as part of a find(‘all’), find(‘list’) query or as part of a containable condition.

You could remove the PHP date() function and pass in variables rather than fixing it to the current year, month or day. The YEAR(). MONTH() and DAY() functions are special MySQL functions that wrap around your Model.date field (or any date field in your DB) field and pulls only the relevant results.

Another good simple CakePHP trick!