2013-03-07 73 views
4

我正在嘗試查找在過去一小時內創建的產品。Magento - 查找在過去一小時內創建的產品

我能夠通過產品的創建日期,像這樣排序:

$collection = Mage::getModel('catalog/product') 
    ->getCollection() 
    ->addAttributeToSelect('*') 
    ->addAttributeToSort('created_at', 'desc'); 

,但我想通過created_at過濾。我試過用addAttributeToFilter('created_at', array('gteq' =>$time));替換上述塊的最後一行,我認爲這是正確的,但我找不到$time的正確格式。

我在這裏的正確軌道,如果是這樣,我應該用什麼格式$time與?

回答

3

您應該使用MySQL時間戳格式。 下面的代碼爲我工作:

$time = "2013-02-06 09:32:23"; // 'yyyy-mm-dd hh:mm:ss' 

$collection = Mage::getModel('catalog/product') 
->getCollection() 
->addAttributeToSelect('*') 
->addAttributeToSort('created_at', 'desc') 
->addAttributeToFilter('created_at', array('gteq' =>$time)); 

你可以很容易地找到一個給定的屬性的格式,通過簡單,它呼應到你的屏幕。 例如:

$collection = Mage::getModel('catalog/product') 
->getCollection() 
->addAttributeToSelect('*') 
->addAttributeToSort('created_at', 'desc'); 

exit($collection->getFirstItem()->getData('created_at'); 

對於產量爲2013年2月6日9時32分23秒(在我的環境)

+0

輝煌,謝謝!這是100%的修復。我不知道爲什麼我在這方面掙扎,現在看起來很明顯。 – Markie 2013-03-07 13:00:30

+0

哈哈沒問題;) – Menno 2013-03-07 13:02:04

+0

「現在我已經知道了,它看起來如此明顯」是我與Magento合作的座右銘。不要爲此感到難過。 – Mike 2013-03-07 14:23:32