0

I have 2 fields in a node and I'm trying to get the expired nodes set in "Start Date" and "End Date" field.

I'm using db_select to get the start and end date values.

How can I add a condition to check if the dates are in range?

Here's my current code:-

$query = db_select('node__field_end_date', 'ed');

$query->leftJoin('node__field_start_date', 'sd', 'sd.entity_id = ed.entity_id');

// Date values showing as a formatted date
$query->addField('ed', 'node__field_end_date', 'date_end');
$query->addField('sd', 'field_start_date_value', 'date_start');

2 Answers 2

1

Something like this should do it:

db_select
  ...
  ->condition('date_field_1', $min_date, '>');
  ->condition('date_field_2', $max_date, '<=')
1

The raw SQL would be something like this,

SELECT startDate, endDate
FROM YourTable
WHERE GETDATE() between startDate and endDate;

You might could do like this with db_select

$query = db_select('node', 'n');
$query->condition(now(), array(node__field_start_date, node__field_end_date), 'BETWEEN');

I hope it helps

5
  • thanks for your response. however, I would like to know how to do it using db_select Commented Oct 8, 2018 at 10:47
  • I have updated the answer now
    – user72672
    Commented Oct 8, 2018 at 11:10
  • @CatherineSalvador Did my anser help you, if yes then please give it the chekmark so that future visitors can find the right answer easily.
    – user72672
    Commented Oct 10, 2018 at 16:31
  • sorry for late response. I haven't tried it yet as I have switched to another project. I'll check on that now. thanks Commented Oct 11, 2018 at 9:49
  • Did it work for you?
    – user72672
    Commented Oct 11, 2018 at 11:02

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.