Domanda

I have a custom collection that I wish to filter by created date and het entries created "yesterday"

Collection Entries

//dates are set in controller using
setCreatedTime(Mage::getModel('core/date')->gmtDate()); 

Created Yesterday (does not work)

//3 products items Yesterday
//below filtering outputs incorrect entries
$collection = Mage::getModel('things/things')->getCollection();

I have tried, but outputs incorrect entries;

//thought strtotime('yesterday') would work..
$collection->addFieldToFilter('created_time', array('gt' => Mage::getModel('core/date')->date('Y-m-d H:i:s', strtotime('yesterday'))));
$collection->addFieldToFilter('created_time', array('gt' => Mage::getModel('core/date')->date('Y-m-d H:i:s', strtotime('-1 day'))));
$collection->addFieldToFilter('created_time', array('from'=> strtotime('-1 day', time()),'to'=> time(),'datetime' => true));
$fromDate = date('Y-m-d H:i:s', strtotime($fromDate));
$toDate = date('Y-m-d H:i:s', strtotime($toDate));
$collection->addFieldToFilter('created_time', array('from'=>$fromDate, 'to'=>$toDate));

Created Today (current Day)(works)

//5 products items today with timestamp 2016-05-01 05:22:53
//below filtering outputs correct entries
$collection = Mage::getModel('things/things')->getCollection();
$collection->addFieldToFilter('created_time', array('gt' => Mage::getModel('core/date')->date('Y-m-d H:i:s', strtotime('today'))));

Created past week (works)

//23 products items with timestamps for this week
//below filtering outputs correct entries
$collection = Mage::getModel('things/things')->getCollection();
$collection->addFieldToFilter('created_time', array('gt' => Mage::getModel('core/date')->date('Y-m-d H:i:s', strtotime('-1 week'))));
È stato utile?

Soluzione

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);

Altri suggerimenti

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)

Magento1

To filter the collection with based on date is bit tricky, because of the timezone, the date we are looking at the admin and the date we have into the database table.

1: Get the From & To date for which you want the filter

$formTxt = '-3 days';
$toTxt = '-1 days';

$from = Mage::getModel('core/date')->gmtDate("m-d-Y", Mage::getModel('core/date')->timestamp($formTxt));
$to = Mage::getModel('core/date')->gmtDate("m-d-Y", Mage::getModel('core/date')->timestamp($toTxt));

2: Now get your Locale zone

$locale = Mage::app()->getLocale()->getLocaleCode();

3: Create From & To date object with TimeZone

$fromObj = Mage::app()->getLocale()->date(null, null, $locale, false);
$fromObj->setTimezone(Mage::app()->getStore()->getConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_TIMEZONE));
$fromObj = $fromObj->set($from, Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT), $locale);

$toObj = Mage::app()->getLocale()->date(null, null, $locale, false);
$toObj->setTimezone(Mage::app()->getStore()->getConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_TIMEZONE));
$toObj = $toObj->set($to, Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT), $locale);

4: Assign timezone to the newly both date objects

$fromObj->setTimezone(Mage_Core_Model_Locale::DEFAULT_TIMEZONE);
$toObj->setTimezone(Mage_Core_Model_Locale::DEFAULT_TIMEZONE);

5: Substract any duration if you want from the "to" date

$toObj->addDay(1)->subSecond(1);

6: Create a date condition array

$dateCondition = array(
            'from'=> $fromObj,
            'to'=> $toObj,
            'locale' => $locale,
            'orig_from' => Mage::helper('core')->formatDate($from),
            'orig_to' => Mage::helper('core')->formatDate($to),
            'datetime' => true
        );

7: At last add the $dateCondition array to collection filter

->addAttributeToFilter('main_table.created_at', $dateCondition)

or

->addFieldToFilter('main_table.created_at', $dateCondition)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top