views:

407

answers:

2

In Magento I'm creating a custom module and would love to be able to filter automatically by the datetime column so that the intial grid listing shows only entities related to "todays" date.

Here is my datetime column:

$this->addColumn('ts', array(
    'header'    => $hlp->__('Activated'),
    'align'     => 'left',
    'index'     => 'ts',
    'type'      => 'datetime',
    'width'     => '160px',
));

I'm think there should be a way for me to just add a filter to the collection like so:

$now = Mage::getModel('core/date')->timestamp(time());
$dateTime = date('m/d/y h:i:s', $now);

$collection = Mage::getModel('mymodule/items')->getCollection()
   ->addFieldToFilter('ts', $dateTime);

But this doesn't work?

Am I using the wrong filter? My "ts" field in the database is a "datetime" field, but the default magento "From: " - "To:" date range selectors don't use hours, minutes, seconds.

Any ideas?

Thanks, Tegan

+3  A: 

This seems to work. I had my time formatted incorrectly.

$now = Mage::getModel('core/date')->timestamp(time());
$dateStart = date('Y-m-d' . ' 00:00:00', $now);
$dateEnd = date('Y-m-d' . ' 23:59:00', $now); 

$collection = Mage::getModel('mymodule/items')->getCollection()
   ->addFieldToFilter('ts', array('from' => $dateStart, 'to' => $dateEnd));
Tegan Snyder
If you want the end of the day, make sure to use 23:59:59, not 23:59:00.
Joseph Mastey
+1  A: 

Tegan, thanks for answering my questions...greetings from SJ!

latvian