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' => 开发者_JS百科'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
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));
I think it should be
$dateEnd = date('Y-m-d' . ' 23:59:59', $now);
instead of
$dateEnd = date('Y-m-d' . ' 23:59:00', $now);
精彩评论