D8 Entity Query - pierregermain/MyDrupal GitHub Wiki

Entity Query with dates

Get past ids

$field = 'field_name';
$now = date('Y-m-d', time());
$start = date('Y-m-d',strtotime($now.'-'.$days.' days'));

// Get past ids using entityQuery.
$ids = \Drupal::entityQuery('entity_type')
       ->condition($field, $now, '<=')
       ->execute();

// Get ids for a specific date
$ids = \Drupal::entityQuery('commerce_product_variation')
      ->condition($field, $now)
      ->execute();

// Get ids from a range of dates
$ids = \Drupal::entityQuery('commerce_product_variation')
      ->condition($field, $now, '<=')
      ->condition($field, $start, '>=')
      ->execute();