Emkey
Emkey

Reputation: 5376

CakePHP Retrieving rows for the current month

I have to following code to retrieve rows with timestamp of today:

    $interactives_presented_today = $this->find('count', 
                    array('conditions' => array( 'Tbsession.timestamp LIKE' => date("Y-m-d") . "%",
                                                'Tbsession.action LIKE' => "%Interactive",
                                                'Tbsession.username' => $user_id,
                                            ) )
                );

Now, I want to retrieve rows with timestamp of this month. Any ideas? Thanks a lot for any help! :)

Upvotes: 0

Views: 5814

Answers (3)

deceze
deceze

Reputation: 522635

$this->find('all', array(
    'conditions' => array('MONTH(Tbsession.timestamp)' => date('n'))
));

Upvotes: 7

Brian Glaz
Brian Glaz

Reputation: 15696

You can use the mysql DATE_FORMAT() function.

$this->find('all',array(
'conditions'=> array('DATE_FORMAT(Tbsession.timestamp,"%m") = "'.date("m").'"')
));

Upvotes: 1

Anh Pham
Anh Pham

Reputation: 5481

this gets all rows that are less than 1 month old:

$this->find('all',array(
   'conditions'=>array($this->alias.'.timestamp >'=>date('Y-m-d', strtotime("-1 month"))
));

if you want to get this month:

$this->find('all',array(
   'conditions'=>array($this->alias.'.timestamp >'=>date('Y-m-00')
));

Upvotes: 2

Related Questions