Magento Set Grid to Filter Automatically by Current Day using Existing Datetime Column in Grid

Magento Set Grid to Filter Automatically by Current Day using Existing Datetime Column in Grid

August 20, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Set Grid to Filter Automatically by Current Day using Existing Datetime Column in Grid

Automatically Filter Magento Grid by Current Day Using Existing Datetime Column

Hello, developers! Today, we’re going to explore a common challenge faced when working with custom modules in Magento—the need to automatically filter a grid listing based on a specific datetime column, specifically the current day.

The Scenario: Filtering by Today’s Date in a Custom Grid

You’ve created a custom module and now want your initial grid listing to display only entities that are associated with today’s date. You have a datetime column named “ts,” but you’re struggling to implement the automatic filtering functionality.

Let’s first examine the existing code provided by Tegan:

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

This code adds a new column named “ts” to your grid, which displays the datetime value stored in the database.

The Attempted Solution: Filtering the Collection

Tegan attempted to filter the collection by adding a condition for the current date and time:

$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);

However, this approach doesn’t work as expected. The addFieldToFilter method requires a specific format for the filter condition.

Solving the Challenge: Using the Correct Filter Format

To solve this issue, we need to understand the structure of the “From:” - “To:” date range selectors in Magento’s built-in grids. These selectors do not include hours, minutes, or seconds, which means that our datetime column must be filtered without considering the time component.

Here are the steps to achieve automatic filtering by the current day:

  1. Retrieve today’s date using Magento’s core/date model:
$now = Mage::getModel('core/date')->timestamp(time());
  1. Extract only the date part from the timestamp, ignoring hours, minutes, and seconds.

  2. Convert the extracted date to a format that matches your “ts” column in the database.

  3. Apply the filter condition to your collection using addFieldToFilter.

The exact code implementation will depend on your specific use case and the format of your “ts” column. However, by following these steps and understanding the requirements for the filter condition, you should be able to achieve automatic filtering based on today’s date.

Conclusion

In this blog post, we discussed a common challenge faced when working with custom grids in Magento—the need to automatically filter entities based on a specific datetime column. We examined the existing code provided by Tegan and identified the underlying issue with the attempted solution.

By understanding the requirements for the filter condition in relation to the built-in date range selectors, we can create an accurate solution that filters the collection based on today’s date.

Remember, the key lies in extracting only the date part from the timestamp and converting it to a format compatible with your “ts” column. This approach respects the structure of Magento’s built-in grids while allowing for automatic filtering based on the current day.

If you found this blog post helpful, feel free to explore our other Magento-related tutorials at Laravel Company. We strive to provide accurate and practical solutions to common challenges faced by Magento developers.

Database Extensions Admin

Our network