Magento过滤器收集的创建时间(今天,昨天,星期,小时等)


9

我有一个自定义集合,希望按创建日期和“昨天”创建的het条目进行过滤

馆藏条目

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

昨天创建(无效)

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

我试过了,但是输出不正确;

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

今天(当前日期)创建(作品)

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

上周创建(作品)

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

Answers:


10

添加到@Ashvin答案..

我在过去一个小时内创建了条目

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

以及如何获得昨天创建的条目;

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

5

我们该如何解决?简单。除非另有要求,否则限制最近24小时内在订单网格中显示的订单量。

示例:- 将app / code / core / Mage / Adminhtml / Block / Sales / Order / Grid.php文件复制到:

app / code / local / Mage / Adminhtml / Block / Sales / Order / Grid.php

编辑以下功能,从此处复制粘贴:

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;

}

用于向您的问题提供更多代码...(今天,昨天,星期,小时等)


go0d作品@ashvin
Amit Bera

嘿! 很好的解决方案!我如何更改它,使其仅需要两个小时前的订单?
弗拉基米尔·德斯波托维奇
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.