Drupal entity query date conditions on created and date fields
A Drupal entity query condition on a date has to use the value format of the field's storage. The base fields created and changed store Unix timestamps in an integer column, while a Datetime field stores an ISO 8601 string such as 2026-11-25T08:15:36 in UTC. Pass that string to a condition on created and MySQL compares it as the number 2026, so a "hide past items" filter matches everything and a "hide future items" filter matches nothing. Check the field's storage type, send a timestamp or a storage-format string to match, and build "now" in UTC.
Where the filter broke
This came up on a group of nonprofit membership sites that share one Drupal distribution. Editors build listing sections out of a paragraph type: pick a content type, choose a field to sort by, and optionally tick "hide past items" or "hide future items". Code behind the paragraph turns those choices into an entity query.
The sort field could be an event's Datetime field, but it could also be the node's created date, which is the natural choice for news. The date filter worked for events and quietly did nothing useful for anything sorted by created. A teammate traced it and shipped the fix; I'm writing it up because the failure is silent and easy to reproduce in any custom listing code.
Why a date string against a timestamp column matches everything
The original code built the comparison value once, in the Datetime storage format, whichever field it was comparing:
$now = new DrupalDateTime('now');
$value = $now->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
$query->condition($sort_field, $value, '>');
For a Datetime field that is close to right. For created, the query ends up as created > '2026-11-25T08:15:36' against an integer column. MySQL and MariaDB don't reject that: they convert the string to a number, reading leading digits until the first character that can't be part of one. The string becomes 2026. Every timestamp since 1970 is larger than 2026, so "hide past items" keeps everything and "hide future items" removes everything.
Nothing errors. MySQL raises a truncation warning, but Drupal doesn't surface warnings from a SELECT, so the page renders a plausible-looking list. That's what makes this kind of bug slow to find: the symptom is a filter that seems not to work, not a stack trace. On PostgreSQL the same query typically fails outright with a type error, which is kinder.
The second bug: "now" in the wrong timezone
The same line had a quieter problem. new DrupalDateTime('now') uses the current user's timezone, or the site's default. Formatting it with DATETIME_STORAGE_FORMAT prints that local wall-clock time, but Datetime fields are stored in UTC. On a site configured for a timezone several hours from UTC, the cut-off between "past" and "future" is off by that many hours: an event that started this morning could still count as upcoming, or one later today could already count as past.
The fix is to create the date in the storage timezone, which Drupal exposes as a constant:
$now = new DrupalDateTime('now', DateTimeItemInterface::STORAGE_TIMEZONE);
A Unix timestamp is the same in every timezone, so this only matters for the string format, but building "now" in UTC up front means both branches are correct without having to think about it.
How to choose the value from the field's storage type
The team's fix asks the entity field manager what type of field it is comparing and returns the matching value. A minimal version of the same idea:
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
function mymodule_date_condition_value(string $entity_type, string $field, DrupalDateTime $date): int|string {
// Accept "field_event_date.value" as well as "field_event_date".
$name = explode('.', $field)[0];
$definitions = \Drupal::service('entity_field.manager')
->getFieldStorageDefinitions($entity_type);
$type = isset($definitions[$name]) ? $definitions[$name]->getType() : NULL;
if (in_array($type, ['created', 'changed', 'timestamp'], TRUE)) {
return $date->getTimestamp();
}
return $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
}
$now = new DrupalDateTime('now', DateTimeItemInterface::STORAGE_TIMEZONE);
$query->condition($sort_field, mymodule_date_condition_value('node', $sort_field, $now), '>');
getFieldStorageDefinitions() includes base fields as well as configurable ones, so the same lookup covers created, changed, a custom timestamp base field and any Datetime field. It is cached by the entity field manager, so calling it while building a query costs very little.
The same fix also went out as a Composer patch for a site still on an older release of the shared module, which is a reminder that a fix in a distribution's custom code has as many deployment paths as there are pinned versions.
What else to watch for with date conditions
Matching on storage type covers the bug that was reported. A few related cases are worth handling while you are in this code:
- Date-only fields. A Datetime field set to "Date only" stores
Y-m-d, not a full date and time. Comparing it against a full timestamp string still works as a string comparison, but "today" behaves differently from what an editor expects: an item dated today compares as earlier than2026-11-25T08:15:36. UseDATE_STORAGE_FORMATfor those if "today" should count as upcoming. - Date ranges. A Date Range field has
valueandend_valuecolumns. For "hide past events" the end date is usually the right column, so a multi-day event stays listed until it finishes. - Caching. A list filtered against "now" changes when time passes, not when content is saved, so it needs a time-based cache max-age as well as cache tags.
What I like about the fix is that it removes a decision from every caller. The listing code no longer has to know whether the editor picked a base field or a configurable one; it asks Drupal and gets the right format.
Key takeaways
- Drupal's
createdandchangedfields store Unix timestamps, while Datetime fields store ISO 8601 strings in UTC, so one comparison value can't serve both. - On MySQL and MariaDB a date string compared with an integer column is read as its leading digits, so the condition silently matches everything or nothing instead of failing.
- Build "now" with
DateTimeItemInterface::STORAGE_TIMEZONE, because Datetime values are stored in UTC and a local-time string shifts the cut-off by the site's offset. - Look up the field's storage type with the entity field manager and return a timestamp or a storage-format string to match.