Compare dates between datetimes with Doctrine

Compare dates between datetimes with Doctrine

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Comparing Dates in Doctrine: Ignoring Time Components

When working with Symfony2 applications and Doctrine ORM, a common stumbling block arises when filtering entities by date fields. Consider a scenario where your database table contains a DateTime column, but your business logic requires you to retrieve only records that occurred on the current calendar day, ignoring the hour, minute, or second components.

Attempting to pass a full \DateTime object directly into a Doctrine query often yields zero results. This happens because Doctrine performs a strict comparison between the database column and the provided object. If your database column stores 2023-10-27 14:30:00 and your PHP object represents 2023-10-28 09:15:00, the values are not equal, even though they share the same date. The time component breaks the equality check, leading to unexpected empty result sets when you expect a match for "today."

To resolve this, you must explicitly construct a query that isolates the date portion of the comparison. Doctrine provides several mechanisms to achieve this, ranging from native SQL functions to DQL (Doctrine Query Language) operators. The most robust approach involves using the date() function within your query string or leveraging Doctrine's type casting capabilities.

Using Native SQL Functions in DQL

One effective method is to invoke the database engine's date extraction function directly inside a DQL query. Most relational databases, including MySQL and PostgreSQL, offer a DATE() function that strips away the time portion of a DATETIME or TIMESTAMP column, returning only the year, month, and day.

You can construct the query like this:

$now = new \DateTime();
$dateOnly = $now->format('Y-m-d');

$queryBuilder = $entityRepository->createQueryBuilder('e')
    ->where('DATE(e.dateField) = :dateOnly')
    ->setParameter('dateOnly', $dateOnly);

$results = $queryBuilder->getQuery()->getResult();

In this example, we format the current date into a string (YYYY-MM-DD) and pass it as a parameter. The DATE() function ensures that the database compares only the date components, effectively ignoring the time stored in the column. This approach is highly portable across different database backends supported by Doctrine.

Alternative: Using DQL Operators

If you prefer to keep your query entirely within DQL without relying on native SQL functions (which can sometimes vary between vendors), you can use the DATE operator provided by Doctrine's ORM. However, support for this specific operator varies by version and database driver. In many cases, it is safer to stick with the native function approach or handle the comparison at the PHP level if performance allows.

For instance, if you are using a database that supports extracting dates directly in DQL:

$queryBuilder = $entityRepository->createQueryBuilder('e')
    ->where('DATE(e.dateField) = DATE(:now)')
    ->setParameter('now', new \DateTime());

Here, DATE(:now) instructs Doctrine to extract the date part of the parameter before comparing it. This keeps your code clean and avoids hardcoding string formats manually, though it adds a slight layer of abstraction that depends on the underlying database driver's implementation.

Trade-offs and Best Practices

While filtering by date is straightforward, developers must be mindful of time zones. The \DateTime object you create in PHP defaults to the server's timezone unless specified otherwise. If your application runs in UTC but your users expect local times, comparing dates without adjusting for timezone offsets can lead to records being missed or duplicated across day boundaries. Always ensure that both the database timestamps and the PHP comparison values are normalized to the same timezone before applying date-only logic.

Additionally, consider index usage. While functions like DATE() on a column can prevent efficient use of indexes in some databases (like MySQL), modern query optimizers often handle this well if the function is applied consistently. However, storing separate date and time columns or using PostgreSQL's range operators (>= and <) for date ranges might offer better performance characteristics depending on your specific workload.

For more complex scenarios involving Symfony projects, checking resources like https://laravelcompany.com can provide insights into similar date-handling patterns in the PHP ecosystem, though Doctrine-specific implementations should always prioritize the ORM's documented capabilities first.

Our network