how to get all products between two dates?
Asked Answered
C

4

5

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 !

Corey answered 16/4, 2012 at 10:13 Comment(0)
B
10

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

Bolin answered 17/4, 2012 at 12:6 Comment(0)
C
17

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,
        ));
Ceroplastic answered 12/9, 2013 at 13:59 Comment(0)
B
10

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

Bolin answered 17/4, 2012 at 12:6 Comment(0)
B
1

There is one issue with your code:

$collection->addFieldToFilter()

should be:

$collection->addAttributeToFilter()
Babysit answered 16/4, 2012 at 12:46 Comment(0)
T
1

I know that the question is little bit old but as it is quite well ranked in search engine results, I will correct the date() function that better take as arguments something like : Y-m-d H:i:s. I hope it will help !

Twofaced answered 13/3, 2014 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.