1

I tried to find the collection that has a range created between 4 hours, but somehow it doesnt show the right collection size, it always returns '0', here's how i use the filter:

$to = date('Y-m-d H:i:s');
$from = date('Y-m-d H:i:s',time() - (3600*4)); //4 hour;
//$from = 2018-04-03 06:29:19;
//$to = 2018-04-03 10:29:19
$collections = $this->mymodel->create()->getCollection()
        ->addFieldToFilter('created_at', ['gteq' => $from])
        ->addFieldToFilter('created_at', ['lteq' => $to]);

here's the data in database:

enter image description here

1

I got entries created within 4 hour :

$from = date('Y-m-d H:i:s', strtotime('-4 hour')); // past 4 hour;
$to = date('Y-m-d H:i:s');

$collections = $this->mymodel->create()->getCollection()
        ->addFieldToFilter('created_at', ['gteq' => $from])
        ->addFieldToFilter('created_at', ['lteq' => $to]);

It works perfectly !

0

Try this

->addFieldToFilter(
            'created_at',
            array('from' => $from, 'to' => $to)
        )->load();
  • Please remove array () syntax. It's not coding standard for magento2 – Rohan Hapani Apr 3 '18 at 5:51

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.