How to find all where items are created last month? or based on months Cakephp3

Hello World,

i was reading the cookbook about find orders which is 30 days ago or last month

     	$testing = $this->Orders->find('all')
	->where(['Orders.created_at >' => new Date('-30 days')])
	->contain(['OrderProducts'])
	->limit(10);
	$testing1 = $this->paginate($testing);
	$this->set(compact('testing', 'testing1'));

i am using
use Cake\I18n\Date;

can anyone guide about how to find it in a correct way? i like to find orders based on last month.

thank you :raised_hands:

What is not working about the solution you have so far?

the array reverted back is more than 30 days, not based on last month

So, if you run it today, you get records created before August 30? Do you have any default paginate settings in this controller that might conflict with the query in question? Is there any difference between what’s in $testing and $testing1?

if i run it today, i get records created last year, as this year no records entered yet. as i got 2 records only

googled another version is new Date( ‘CURRENT_DATE - INTERVAL 1 MONTH’)])

            $testing = $this->Orders->find('all')
	->where(['Orders.created_at >' => new Date( 'CURRENT_DATE - INTERVAL 1 MONTH')])
	->contain(['OrderProducts'])
	->limit(10);
	$testing1 = $this->paginate($testing);
	$this->set(compact('testing', 'testing1'));

result is the same, only return back all records

The CURRENT_DATE - INTERVAL 1 MONTH version will definitely not work. That’s a MySQL structure, not something that the PHP code will turn into a usable date. But new Date('-30 days') does give me the correct date, so I don’t see any reason why it wouldn’t work for you. (FrozenDate::now()->subDays(30) would be another way to go, but should be equivalent.)

How about the other two questions that I asked in my previous reply?

Good morning Zuluru, and thanks, i give it a try, CURRENT_DATE - INTERVAL 1 MONTH version i found it on google.

your questions :
Do you have any default paginate settings in this controller that might conflict with the query in
question?

i did not touch the paginate default settings yet. my teacher did not teach about paginate stuff yet.

   Is there any difference between what’s in `$testing` and `$testing1` ?

i checked the debug mode variables and $testing and $testing1 showed back both in same arrays, in the variables. i tried -30days, -60days, -300days

Thank you very much Zuluru, you are much better skilled than my Teacher in cakephp!!!

:raised_hands: :raised_hands: :raised_hands: