magentomagento-1.4

how to get all products between two dates?


how to get all products between two dates like last month products, this month products , last week products and this week products etc.

i tried with this:

// current day to start with
$start = mktime(0,0,0,date('m'), date('d'), date('Y'));;

// calculate the first day of last month
$first = date('YYYY-MM-DD',mktime(0,0,0,date('m',$start) - 1,1,date('Y',$start)));

// calculate the last day of last month
$last = date('YYYY-MM-DD',mktime(0, 0, 0, date('m') -1 + 1, 0, date('Y',$start)));



   if($filter == "lastmonth"){

    $collection = Mage::getModel('catalog/product')->getCollection();
    $collection->addAttributeToFilter('updated_at', array('gteq' =>$first));
    $collection->addAttributeToFilter('updated_at', array('lteq' => $last));


}

but i am not able to get the result :( any help ?

Modified after Daniel response !


Solution

  • I tried your code and had to swap 'lteq' and 'gteq' to make it work. The $fromdate is the lower number so you are searching for dates greater than that number.

    Also you must remember to format the dates as MySQL likes it; date('Y-m-d').

    PS. See the comparison operators for a full list