Magento - Search for products created in the last hour

I am trying to find products created in the last hour.

I can sort the products by creation date this way:

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

But I would like to filter out the created_at file. I tried replacing the last line of the specified block with addAttributeToFilter('created_at', array('gteq' =>$time)); which, I think, should be right, but I cannot find the correct format for $time .

I am on the right track here, and if so, in what format should I feed $time with?

+4
source share
1 answer

You must use the MySQL timestamp format. The following code worked for me:

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

You can easily find the format of this attribute by simply dropping it on your screen. For instance:

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

The result for this was 2013-02-06 09:32:23 (in my environment)

+3
source

All Articles