0

I want to get the list of the nodes, whose content type is Donors, where the date in the field_data_field_donor_date_of_birth matches today (except the year).

How can I achieve it using a dynamic query or the EntityFieldQuery class?

This is the code I tried so far.

$query = db_select('node', 'node');
$query->fields('node', ['nid','title'])
  ->condition('node.status', 1)
  ->condition('node.type', 'CONTENT_TYPE_DONOR');

$query->innerJoin('field_data_field_donor_date_of_birth', 
 'field_donor_date_of_birth'
, 'field_donor_date_of_birth.entity_id = node.nid');
$query->condition('field_donor_date_of_birth.field_donor_date_of_birth_value', $birth_date, '=');

$result = $query->execute();
3
  • 1
    Since the question is about code to write, you should show the code you wrote so far. If there is anything you don't understand in the described task, you need to clearly state what that is.
    – apaderno
    Commented Sep 20, 2018 at 12:51
  • The question is also missing details. It's asking how to get the list of users whose birthday is today, but it doesn't say where the birthday is stored. (I assume it's an entity field, but the question doesn't say which field that is exactly.)
    – apaderno
    Commented Sep 20, 2018 at 12:56
  • @kiamlaluno: Thanks. I have added code for demonstration
    – Amit Sedai
    Commented Sep 21, 2018 at 12:09

1 Answer 1

1

I was able to query the date by using a where clause instead of condition. Example, replace

 $query->condition('field_donor_date_of_birth.field_donor_date_of_birth_value', $birth_date, '=');

with

$time = format_date(time(), 'custom', 'm-d');
$query->where("DATE_FORMAT(field_donor_date_of_birth.field_donor_date_of_birth_value, '%m-%d') = '$time'");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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