0

I have Code for getting all order But I want date filter in Object-manager. How Can I add date filter in it. I tried but I got Blank array. I am using Object manager in the script so I am suitable with this Way.

 $startDate = date("Y-m-d h:i:s",strtotime('2018-1-1')); // start date
 $endDate = strtotime("Y-m-d h:i:s", strtotime('2018-12-12')); // end date              

 $objectManager = \Magento\Framework \App\ObjectManager::getInstance(); 

$orderDatamodel = $objectManager->get('Magento\Sales\Model\Order')->getCollection()->addAttributeToFilter('created_at', array('from'=>$startDate, 'to'=>$endDate)); 
 $orderDatamodel->getSelect()->order('entity_id DESC');
  • Can you give me the whole code? Thank in advance. – Krunal Pandya Dec 28 '18 at 9:18
  • Sorry folks Code ok but the problem in data. – Krunal Pandya Dec 28 '18 at 9:44
0

You can try this.

$startDate = date("Y-m-d h:i:s",strtotime('2018-12-27')); // start date
$endDate = date("Y-m-d h:i:s", strtotime('2018-12-28')); // end date   

 $objectManager = \Magento\Framework \App\ObjectManager::getInstance(); 

$orderDatamodel = $objectManager->get('Magento\Sales\Model\Order')->getCollection()->addAttributeToFilter('created_at', array('from'=>$startDate, 'to'=>$endDate)); 
 $orderDatamodel->getSelect()->order('entity_id DESC');
| improve this answer | |
  • I think our code is the same. – Krunal Pandya Dec 28 '18 at 9:24
  • yes. end date was wrong.It should be $endDate = date("Y-m-d h:i:s", strtotime('2018-12-28')); – Pritam Info 24 Dec 28 '18 at 9:25
  • Sorry to say but still got an empty array. – Krunal Pandya Dec 28 '18 at 9:28
  • 1
    where did you using the code, Out of Magento code base? please confirm, Make sure you change the Form and To date. Also, check any order actually exist or not within the date range. – Pritam Info 24 Dec 28 '18 at 9:33
  • yes orders are also there and when you remove "->addAttributeToFilter('created_at', array('from'=>$startDate, 'to'=>$endDate));" part code run prefactlly. – Krunal Pandya Dec 28 '18 at 9:35

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.