문제

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

도움이 되었습니까?

해결책

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)'
    )
));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top