3

I have custom collection and I want to check current date available or not between start_date and end_date.

$currentDate = date("Y-m-d 00:00:00");

$collection = Mage::getModel('test/validity')->getCollection()->addFieldToFilter('start_date',array('gteq'=>$currentDate))
->addFieldToFilter('end_date',array("lteq" => $currentDate));

But this above code is not work.what I do ?

4
->addFieldToFilter('start_date',array('gteq'=>$currentDate))
->addFieldToFilter('end_date',array("lteq" => $currentDate));

Adding two filters like this translates to "start date >= today AND end data <= today", while what you want is OR.

To combine filters with "or", do it like this in a single call:

->addFieldToFilter(
    ['start_date', 'end_date'],
    [['gteq' => $currentDate], ['lteq' => $currentDate]]
);
4

@fschmengler

Pretty sad that your ignorance gave me a thumb down. Here you have the correct code:

$fromDate = '2016-08-01 00:00:00';
$toDate = '2016-08-31 23:59:59';

$products = Mage::getModel('catalog/product')->getCollection()
->addFieldToFilter('start_date', array('from' => $fromDate, true))
->addFieldToFilter('end_date', array('to' => $toDate, true));

Your code example is not in the current Magento syntax, which isnt fine. I wouldnt use it. Hope you give me a thumb up now.

  • Sorry, can't. I don't know what you mean with "not the current Magento syntax", my answer shows how to combine filters for multiple fields with OR. Yours shows how to combine filters with AND. Which is still not an answer to the question, which is how to filter dates NOT within the range. – Fabian Schmengler Aug 14 '16 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.