2013-03-07 74 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