How To

Magento 2: Get Orders Collection Between a Date Range

Hello Magento Friends,

In today’s Magento 2 tutorial, we will find out How to Get Orders Collection Between a Date Range in Magento 2.

The admin uses order collection to get order details and smoothly fulfill the orders. Sometimes the admin does not want all order collection but needs only specific orders based on filter options.

Suppose you want all orders between a specific date range. You can Get Order Collection Between Specific Date Range in Magento 2 using the below method.

How to Get Orders Collection Between a Date Range in Magento 2?

Use the below code in your Root file

<?php
use Magento\Framework\App\Bootstrap;
ini_set('display_errors', TRUE); 
ini_set('display_startup_errors', TRUE);

require __DIR__ . '/../app/bootstrap.php';

$params = $_SERVER;

$bootstrap = Bootstrap::create(BP, $params);

$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$now = new \DateTime();

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

$OrderFactory = $objectManager->create('Magento\Sales\Model\ResourceModel\Order\CollectionFactory');

$orderCollection = $OrderFactory->create()->addFieldToSelect(array('*'));

$fromdate=$now->format('2020-05-01 H:i:s');

$todate=$now->format('2020-06-01 H:i:s');

$orderCollection->addFieldToFilter('created_at', ['lteq' => $todate])
   ->addFieldToFilter('created_at', ['gteq' => $fromdate]);

echo "<pre>"; 
print_r($orderCollection->getData());

?>

Conclusion:

This way, you can get all orders from a specific range of dates in Magento 2. Alternatively, Get all Orders of Customers by Email id in Magento 2. If you face difficulty retrieving the order collection of a specific date range, you can connect with me through the comment section. 

Happy Coding!

Click to rate this post!
[Total: 2 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

2 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

2 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

2 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

3 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

5 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

6 days ago