Koleksi Filter Magento dengan membuat waktu (hari ini, kemarin, minggu, jam dll)

9

Saya memiliki koleksi khusus yang ingin saya filter berdasarkan tanggal dibuat dan het entri dibuat "kemarin"

Entri Koleksi

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

Dibuat Kemarin (tidak berfungsi)

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

Saya sudah mencoba, tetapi menampilkan entri yang salah;

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

Dibuat Hari Ini (Hari ini) (berfungsi)

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

Dibuat minggu lalu (karya)

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

Jawaban:

10

Untuk menambah jawaban @Ashvin ..

Saya mendapat entri yang dibuat dalam satu jam terakhir

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

dan bagaimana saya mendapat entri yang dibuat kemarin;

$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);
BENN1TH
sumber
5

Bagaimana kita menyelesaikannya? sederhana. membatasi jumlah pesanan yang disajikan dalam kisi pesanan selama 24 jam terakhir, kecuali diminta sebaliknya.

Contoh: - Salin file aplikasi / kode / inti / Penyihir / Adminhtml / Blok / Penjualan / Pesanan / Grid.php ke:

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

Edit fungsi berikut, salin-tempel dari sini:

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;

}

gunakan kode lain untuk mengajukan pertanyaan Anda ... (hari ini, kemarin, minggu, jam, dll)

Ashvin Monpara
sumber
go0d works @ashvin
Amit Bera
Hei! Solusi hebat! Bagaimana saya bisa mengubahnya sehingga hanya dibutuhkan pesanan dari dua jam yang lalu?
Vladimir Despotovic