i want to display at home page how many user have buyed in the last 24hours at our shop

this is my code but the result can never be.

$dateToday = date('Y-m-d');
$dateyesterday = date("Y-m-d H:m:s", strtotime('-24 hours'));
$collection = Mage::getResourceModel('sales/order_item_collection')->addAttributeToSelect('product_id');
$collection->addAttributeToFilter('created_at', array('gteq' =>$dateyesterday));
$collection->addAttributeToFilter('created_at', array('lteq' => $dateToday)); ?>
<?php if ($collection->count() > 0){?>

<div style="float:right; width: 290px; height: 60px; border:#95dc18 dotted; background-color:#3e9de1;">
    <p style="font-family:'Lucida Sans Unicode', 'Lucida Grande', sans-serif; font-size:10px;">In den letzten 24 Stunden schenkten uns
        <span style=" font-weight:600;"><?php echo $collection->count();?></span>
        Kunden ihr Vertrauen und haben bei uns eingekauft. <br />
        Vielen Herzlichen Dank: <span style=" font-family:Tahoma, Geneva, sans-serif; font-weight:900;">Frau BlueB</span>
<?php } ?>

thanks for any help

i found now a solution that works

$dateToday = date("Y/m/d");
                    $dateYesterday = date("Y/m/d", strtotime('-24 hours'));

                        $collection = Mage::getModel('sales/order')
                            ->addAttributeToFilter('created_at', array('from'=>$dateYesterday, 'to'=>$dateToday));

a little thing dont work how i want. the script count only the orders from yesterday. but not the orders from today do anyone hacve a solution for this

  • What the error you facing? Jan 2, 2017 at 4:39
  • no error but the result is not the result that must be there... e.g. result is 12 but in the last 24hours i have only 2 orders
    – Andi Wyder
    Jan 2, 2017 at 11:37

2 Answers 2


Try this:

$toDate = date('Y-m-d');
$fromDate = date("Y-m-d H:m:s", strtotime('-24 hours'));     

/* Get the collection */
$orders = Mage::getModel('sales/order')->getCollection()
    ->addAttributeToFilter('created_at', array('from'=>$fromDate, 'to'=>$toDate));
    //->addAttributeToFilter('status', array('eq' => Mage_Sales_Model_Order::STATE_COMPLETE)); // uncomment this code to get order with status completed
echo count($order);
  • Is timezone effect on order collection by date like frontend time zone and admin time zone both defferent defferent Jan 2, 2017 at 13:45
  • yeah right.. but im the admin and im in the same timezone when i go to frontend. so for me must be the result the same i think
    – Andi Wyder
    Jan 2, 2017 at 14:11
  • this code destroys the complete layout and after code nothing to display
    – Andi Wyder
    Jan 2, 2017 at 14:13
  • is it displaying any fatal error?
    – Piyush
    Jan 2, 2017 at 14:16
  • no error.. page loads till this code and after code only white.. but one question is it possible that this code count how many products sell in the last 24 hours
    – Andi Wyder
    Jan 3, 2017 at 12:29

Use this code and make sure you have order between same date range

$dateToday = date('d/m/Y');
$dateyesterday = date("d/m/Y", strtotime('-24 hours'));
$collection= Mage::getResourceModel('sales/order_item_collection')->addAttributeToSelect('*');
$collection->addAttributeToFilter('created_at', array('from'=>$dateyesterday, 'to'=>$dateToday));
  • i used your code but the result is wrong i have yesterday 2 orders and after insert your code it shows the result is 12
    – Andi Wyder
    Jan 2, 2017 at 11:20
  • Please check now. I have changed date format.Please mark as answer if it's working for you.
    – Ashvini K
    Jan 2, 2017 at 12:57
  • dont work. with your code nothing displayed
    – Andi Wyder
    Jan 2, 2017 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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