Sharma Vikram
Sharma Vikram

Reputation: 2480

search result in month using mysql or cakephp

Hi everyone i have one question search result month in using MySql or Cakephp my table name is state_supplies and data are

enter image description here

MySql query is:

SELECT * FROM `state_supplies` WHERE created=DATE_FORMAT(NOW(), '%m')

Cakephp search code is

$this->StateSupply->find('all', array(
                      'conditions'=> array(
                        'unitid'=>$this->Session->read('Auth.User.unitid'),
                        'created'=>'MONTH(CURDATE())'
                      )                 
                    )
);

Upvotes: 1

Views: 125

Answers (1)

Ahosan Karim Asik
Ahosan Karim Asik

Reputation: 3299

Try this:

In Mysql:

$first_day_this_month = date('Y-m-01 H:i:s'); // hard-coded '01' for first day
$last_day_this_month  = date('Y-m-t H:i:s');
$sql="SELECT * FROM state_supplies WHERE created between '$first_day_this_month' and '$last_day_this_month' "

Cakephp code

Details

Update code for right answer

 $this->StateSupply->find('all',array(
                'conditions'=>array(
                    'unitid'=>$this->Session->read('Auth.User.unitid'),
                    'crop'=>$this->request->data['StateSupply']['crop'],
                    'state'=>$this->request->data['StateSupply']['state'],
                    'created BETWEEN ? AND ?'=>array(date('Y-m-01',strtotime('this month')),date('Y-m-t',strtotime('this month')))
                        )                   
                    )
                );   

Upvotes: 2

Related Questions