Magento – Custom Date Range in Admin Dashboard

dashboarddatemagento-1

I am working on a task to modify Date Range on Dashboard chart. Data chart will display on custom date range selection (Today/Yesterday/This Week/Last Week/This Month/Last Month).

I have extend the module from core (\app\code\core\Mage\Reports\Model\Resource\Order\Collection.php) to local (\app\code\local\Andi\Dashboard\Model\Reports\Resource\Order\Collection.php). I make changes to the getDateRange function.

Custom Admin Dashboard Date Range

I'm adding

case '2d':
            $dateEnd = Mage::app()->getLocale()->date();
            $dateEnd->addHour(1);
            $dateStart = clone $dateEnd;
            $dateStart->subDay(2);
            break;

for yesterday data.

2d means yesterday.

The condition is that the data shown today is the data of today's date only and does not include data from yesterday. Data shown yesterday was a day of data without the data the day before yesterday and today. So also with the data of the Week, Last Week, This Month, Last Month, This Year and Last Year.

I have debug it and get the following data:

$dateEnd = Mage::app()->getLocale()->date(); is Feb 12, 2015 7:56:15 PM
$dateEnd->addHour(1);                        is Feb 12, 2015 8:56:50 PM
$dateStart->subDay(2);                       is Feb 10, 2015 8:57:34 PM

How do I make by using addHour, subHour, addDay, subDay, addWeek, subWeek, addMonth, subMonth, addYear and subYear. Or can use this?

$current_time = date("Y-m-d H:i:s", Mage::getModel('core/date')->timestamp(time()));
$yesterday = date("Y-m-d", Mage::getModel('core/date')->timestamp(time()-(60*60*24)));

Best Answer

The call Mage::app()->getLocale()->date() gives you an object of type Zend_Date. So your question regarding the additional methods like addHour and subDay deal with the Zend class Zend_Date, and not that much with Magento. The API page (http://framework.zend.com/manual/1.12/en/zend.date.overview.html) mentions the magic calls of sub() and add(), so my first reaction is that it should it be working as you mention it. However, I assume you have already tested this, and that it does not work in the way you want it to work. To overcome that, I would say you simply need to create a new class in your own custom Magento module (a helper for instance) that extends from Zend_Date so that you can implement those features you want to. Does that help?

Related Topic