如何在两个日期之间获得所有产品?

时间:2020-12-21 21:28:57

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 !

修改后的Daniel回复!

4 个解决方案

#1


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.

我尝试了你的代码,不得不交换“lteq”和“gteq”来让它工作。$fromdate是较低的数字,因此您正在搜索大于该数字的日期。

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

此外,您还必须记住按MySQL的要求格式化日期;日期(“Y-m-d”)。

PS. See the comparison operators for a full list

请参阅比较操作符以获得完整的列表

#2


16  

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.

1)首先,你需要将你的约会日期从YYYY-MM-DD改为yy -m-d。这将返回magento记录的日期格式。

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

#3


1  

There is one issue with your code:

你的代码有一个问题:

$collection->addFieldToFilter()

should be:

应该是:

$collection->addAttributeToFilter()

#4


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 !

我知道这个问题有点过时了,但由于它在搜索引擎结果中的排名相当好,我将更正date()函数,它最好作为参数,比如:Y-m-d H: I:s。我希望它能有所帮助!

#1


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.

我尝试了你的代码,不得不交换“lteq”和“gteq”来让它工作。$fromdate是较低的数字,因此您正在搜索大于该数字的日期。

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

此外,您还必须记住按MySQL的要求格式化日期;日期(“Y-m-d”)。

PS. See the comparison operators for a full list

请参阅比较操作符以获得完整的列表

#2


16  

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.

1)首先,你需要将你的约会日期从YYYY-MM-DD改为yy -m-d。这将返回magento记录的日期格式。

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

#3


1  

There is one issue with your code:

你的代码有一个问题:

$collection->addFieldToFilter()

should be:

应该是:

$collection->addAttributeToFilter()

#4


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 !

我知道这个问题有点过时了,但由于它在搜索引擎结果中的排名相当好,我将更正date()函数,它最好作为参数,比如:Y-m-d H: I:s。我希望它能有所帮助!