Question

let's say that I have a database table called users in a cakephp application which contains a bunch of users and in that table I have a field called date which contains the registration date of each user. Let's say that I wanna make a find query to get all the users registered last month for example. Something like:

$last_month_users = $this->User->find('all', array(
    'conditions' => array(
        'User.date' => 'last month'
    )
));

Something like this doesn't work. Any idea how I would do this please?

Thank you

Was it helpful?

Solution

If you're using mysql, you're looking to add in your statement:

WHERE MONTH(User.date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)

I believe you can do this in cakePHP by doing the following:

$last_month_users = $this->User->find('all', array(
    'conditions' => array(
        'MONTH(User.date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)'
    )
));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top