Friday, July 27, 2012

Getting records between dates using addAttributeToFilter

To get records between two dates using addAttributeToFilter, use code like this:

$collection->addAttributeToFilter('start_date', array('date' => true, 'to' => $todaysDate))
    ->addAttributeToFilter('end_date', array(array(
        array('date' => true, 'from' => $todaysDate),
        array('null' => true)
    ), 'left');

Note: Where $collection is collection from your model.
Note: $todaysDate can be replaced by date of your choice.

That's all.. cheers.. :)

No comments: