【问题标题】:Magento - look up products created in the last hourMagento - 查找最近一小时内创建的产品
【发布时间】:2013-03-07 12:38:07
【问题描述】:

我正在尝试查找过去一小时内创建的产品。

我可以像这样按创建日期对产品进行排序:

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

但我想按 created_at 过滤。我尝试用addAttributeToFilter('created_at', array('gteq' =>$time)); 替换上面块的最后一行,我认为这一定是正确的,但我找不到$time 的正确格式。

我在正确的轨道上,如果是,我应该以什么格式输入$time

【问题讨论】:

    标签: magento product datecreated


    【解决方案1】:

    您应该使用 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-02-06 09:32:23(在我的环境中)

    【讨论】:

    • 太棒了,谢谢!这是一个100%的修复。我不知道我为什么要为此苦苦挣扎,现在看起来很明显。
    • “现在我已经弄清楚了,它看起来很明显”是我在使用 Magento 时的座右铭。不要为此感到难过。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多