9

I need to select entities with specific dates.

How should the following be formatted. Do I need to parse 15-May-2010

Also how I get the error page.

function events2() {

$query = new EntityFieldQuery();
$query
  ->entityCondition('entity_type', 'node', '=')
  ->propertyCondition('status', 1, '=')
  ->propertyCondition('type', 'event')  
  ->propertyCondition('field_event_date', '15-May-2010', '=');

$result = $query->execute();

 return $result;

}
  • 1
    This can not be answered as it stands because we do not know the field type for field_event_date. – user49 Aug 15 '11 at 19:54
10

With Drupal 7 and Date module 2.2:

$query = new EntityFieldQuery;
$result = $query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'event')
->propertyCondition('status', 1) // Getting published nodes only.
->fieldCondition('field_dates', 'value2', date("Y-m-d"), '<') // end date before today
->execute();
  • This worked for me when I changed the 'value2' to 'value'. – Craig Aug 22 '13 at 15:54
  • 1
    Note that 'value' is for start date and 'value2' is for end date, if configured. – Mario Awad Feb 19 '14 at 14:35
2

I haven't used EntityFieldQuery, but looking at the code suggests that you'll need to make sure that field_event_date is stored as a MySQL DateTime field, and that the format of the argument is in one of the preferred formats:

As a string in either 'YYYY-MM-DD' or 'YY-MM-DD' format. A “relaxed” syntax is permitted here, too. For example, '98-12-31', '98.12.31', '98/12/31', and '98@12@31' are equivalent.

0

Here is the code for checking date range using BETWEEN keyword,

$month = $form_state['values']['month'];
$year = $form_state['values']['year'];
$num_padded = sprintf("%02d", $month);
$first_day = date($year.'-'.$num_padded.'-01 00:00:00'); 
$last_day =  date("Y-m-t 23:59:59", strtotime($first_day));
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
            ->entityCondition('bundle', 'YOUR_CONTENT_TYPE')
            ->fieldCondition('DATE_FIELD', 'value', array($first_day,$last_day), 'BETWEEN');

Not the answer you're looking for? Browse other questions tagged or ask your own question.