Magento Filter collection by create times (today, yesterday, week, hour etc)
To add to @Ashvin answer..
I got entries created within the past hour
$things = Mage::getModel('things/things')->getCollection();
$things->addFieldToFilter('things_type', 'view');
$fromDate = date('Y-m-d H:i:s', strtotime('-1 hour'));
$toDate = date('Y-m-d H:i:s', strtotime(now()));
$things->addFieldToFilter('created_time', array(
'from' => $fromDate,
'to' => $toDate,
'date' => true,
));
return count($things);
and how I got yesterdays created entries;
$now = Mage::getModel('core/date')->timestamp(time());
$dateStart = date('Y-m-d' . ' 00:00:00', $now);
$dateEnd = date('Y-m-d' . ' 23:59:59', $now);
$things = Mage::getModel('things/things')->getCollection();
$things->addFieldToFilter('things_type', 'view');
$things->addFieldToFilter('created_time', array('from' => $dateStart, 'to' => $dateEnd));
return count($things);
How do we solve it? simple. limiting the amount of orders presented in the orders grid for the last 24 hours, unless requested otherwise.
Example:- Copy the app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php file to:
app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php
Edit the following function, copy-paste from here:
protected function _prepareCollection() {
$collection = Mage::getResourceModel($this->_getCollectionClass());
######################## FILTER BY LAST DAY ######################
$now = Mage::getModel('core/date')->timestamp(time());
$filter = $this->getParam($this->getVarNameFilter(), null); //important - check for other requested grid-filters before filtering today's orders
$dateStart = date('Y-m-d' . ' 00:00:00', $now);
$dateEnd = date('Y-m-d' . ' 23:59:59', $now);
$postData = Mage::app()->getRequest()->getPost();
if (empty($filter)) {
$collection->addFieldToFilter('`main_table`.created_at', array('from' => $dateStart, 'to' => $dateEnd));
}
##################################################################
$collection->getSelect()->group('entity_id');
$this->setCollection($collection);
return $this;
}
use to more code to Your ask question... (today, yesterday, week, hour etc)