1

No matter what i try i cant get EntityFieldQuery to work i have a date and i need to check if it is between the start date and end date of a date field that is set to unlimited values.. it should be at least between any one.. here is what i got.. can anybody help me?

UPDATE 10/26/2016: So from what I found doing some more googling to do what I want I need to use delta_group so I added it to the code below but it doesn't seem to be working. Anybody have any idea what I'm doing wrong?

    $myDate = date('Y-m-d H:i:s');
    $query = new EntityFieldQuery;
    $query->entityCondition('entity_type', 'node')
          ->entityCondition('bundle', 'caregiver_db')
          ->propertyCondition('status', 1)
          ->fieldCondition('field_available_dates', 'value', $myDate, '>=', 0)
          ->fieldCondition('field_available_dates', 'value2', $myDate, '<=', 0);

    $results = $query->execute();
  • you're using the same date twice - this query will only match dates values like Y-m-d 00:00:00 – rémy Oct 26 '16 at 11:31
  • it needs to be the same date.. because the same date needs to be between the start date and the end date.. and i've tried Y-m-d H:i:s already and that didnt work.. btw the date field is set to date.. as apposed to date (ISO) or date (unix) – RJX Oct 26 '16 at 18:08
  • so >= Y-m-d 00:00:00 and <= Y-m-d 23:59:59 does not work ? and you have entries in the database ? – rémy Oct 26 '16 at 19:57
  • yes there are several entries.. i've tried this specific string before "2016-10-30T14:02Z" and it returned an node but that node didnt have a date entry for the 30th of October 2016.. so i'm at a complete loss.. this just doesn't make any kind of sense – RJX Oct 26 '16 at 22:04
  • so actually i just tried Y-m-d H:i:s again and it seems to be working now.. i'm not sure what was wrong before.. maybe a typo? thanx for your help though – RJX Oct 26 '16 at 22:09
1

So it looks like the final issue I was having was the operators I was using. Had to go <= on value and >= value2. I had it the other way around before. Here is the code that worked.

$myDate = date('Y-m-d H:i:s');
$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'node')
      ->entityCondition('bundle', 'caregiver_db')
      ->propertyCondition('status', 1)
      ->fieldCondition('field_available_dates', 'value', $myDate, '<=', 0)
      ->fieldCondition('field_available_dates', 'value2', $myDate, '>=', 0);

$results = $query->execute();
| improve this answer | |
0

You could also use the BETWEEN operator, like:

$fromDate = date('Y-m-d') . ' 00:00:00';
$toDate = date('Y-m-d') . ' 23:59:59';
$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'node')
      ->entityCondition('bundle', 'caregiver_db')
      ->propertyCondition('status', 1)
      ->fieldCondition('field_available_dates', 'value', array($fromDate, $toDate), 'BETWEEN');
$results = $query->execute();

Also just go for the value if it's not a start/enddate field.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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