Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 !

like image 814
atif Avatar asked Apr 16 '12 10:04

atif


1 Answers

1) First of all you need to change you date formate from 'YYYY-MM-DD' to 'Y-m-d'. This will return a date formate which magento records have.

2) There is a special condition for date as bellow mention with your example.

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

To.

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('updated_at', array(
        'from' => $first,
        'to' => $last,
        'date' => true,
        ));
like image 179
Rakesh Avatar answered Oct 03 '22 03:10

Rakesh