Magento 1.9 Orders Collection – Get All in Current Day Range

magento-1.9order-collectionorder-totals

Trying to gather all the orders (current day) and sum totals

Updated:

    $format = Mage::getModel('core/date')->date('Y/m/d');
    $from = $format.' 00:00:00';
    $now  = Mage::getModel('core/date')->date('Y/m/d H:m:i');

    $torders = Mage::getModel('sales/order')
        ->getCollection()
        ->addAttributeToSort('increment_id', 'DESC')
        ->addFieldToFilter('state',Array('neq'=>Mage_Sales_Model_Order::STATE_NEW))
        ->addAttributeToFilter('created_at', array('from' => $from,'to' => $now))
        ;

everything is fine except one little thing, one of the orders seems to be not in collection.

Checked all customer_emails and the order created just before midnight (00:14:46) is not included in collection for some reason.
suppose to be 127 order while getSize() is saying 126 regardless what filters used etc.

Any ideas ?

Best Answer

Try this

$from = new DateTime('2017-08-29 00:00:00');
$from = $from->format('Y-m-d H:i:s');

$to = new DateTime('2017-08-29 23:59:9');
$to = $to->format('Y-m-d H:i:s');

$orders = Mage::getModel('sales/order')->getCollection()
    ->addAttributeToFilter('created_at', array('from'=>$from, 'to'=>$to))
    ->addAttributeToFilter('status', array('neq' => Mage_Sales_Model_Order::STATE_NEW));
    ->addAttributeToSelect('*');
Related Topic