Solved

Drupal db_select with BETWEEN not working

Posted on 2011-10-01
3
2,008 Views
Last Modified: 2012-05-12
I am trying to build a list of nodes that have a publication date between two days ago and now.  I tried this:

  $query = db_select('node', 'n');
  $query->join('field_data_field_pubdate', 'f', 'n.nid = f.entity_id'); //JOIN node with pubdate
  $query->fields('n', array('nid', 'created', 'title'));
  $query->addField('f', 'field_pubdate_value', 'pubdate');
  $query->condition('n.type', variable_get('ain_sitemap_node_types', array_keys(node_type_get_names())));
  $query->condition('n.status', NODE_PUBLISHED);
  $query->condition('f.field_pubdate_value', array(REQUEST_TIME - 172800, REQUEST_TIME), 'BETWEEN');
  $query->addTag('node_access'); 
  $query->orderBy('f.field_pubdate_value', 'DESC'); 
  $query->range(0, 1000); 
  $nodes = $query->execute()->fetchAll(); 

Open in new window


But it's not working - now, it's just the one row with the BETWEEN clause (Line 7) - if I replace with this:

$query->condition('f.field_pubdate_value', REQUEST_TIME - 172800, '>=');

Open in new window


It works fine.  But this returns nodes with a pubdate value greater than right now, which I don't want.
0
Comment
Question by:michaelgiaimo
  • 2
3 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36898162
With time values, I would do two anyway. So:

$query->condition('f.field_pubdate_value', REQUEST_TIME - 172800, '>=');
$query->condition('f.field_pubdate_value', REQUEST_TIME, '<=');

Open in new window

0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 36898174
By the way, your syntax appears correct per documentation: http://drupal.org/node/310086
Not sure why it did not work. Might be something with how it is handling the conversion of the date/time when it hits MySQL. You may need to format those, so they pass in as YYYY-MM-DD formatted strings.
0
 

Author Comment

by:michaelgiaimo
ID: 36898222
Yeah, that was it - field_pubdate_value was a mysql date field, while REQUEST_TIME returns the Unix time.  Good catch.  Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now