cakephp-grouped-data

[Snippet] Retrive Data Grouped in Year, Month and Day on CakePHP 2.x

A small note useful for cakephp developers and especially for myself. When you want to took the record from the database and grouped into day, month and year, you can use the following functions. Simple but useful. You do not need to write a lot of codes just to retrieve the data in January 2015, for example.

I use the following snippet.

//-- for day
$this->set('data',$this->YourMODEL->find('all',array('conditions'=>array('DAY(YourMODEL.created)' => date('d')))));


//-- for month
$this->set('data',$this->YourMODEL->find('all',array('conditions'=>array('MONTH(YourMODEL.created)' => date('m')))));

//-- for year
$this->set('data',$this->YourMODEL->find('all',array('conditions'=>array('YEAR(YourMODEL.created)' => date('Y')))));

Requirement: You must have a table created and modifield field. This field will automatically be filled by cakephp. Its contents are complete date with time.

Hope this snippet help you also. Thanks.

No Responses

Show all responses

Write a response