Reputation: 99
Hi i'm trying to do an db_select to compare date. However the field is of type datetime . I can't figure out how to compare between two date. A sample date from database is as follows 2015-10-21 00:00:00. Any help would be great.
$query->condition('dp.field_parution_value', 'DATE(2015-08-01)', '>');
$query->condition('dp.field_parution_value', 'DATE(2015-12-31)', '<');
Upvotes: 2
Views: 3158
Reputation: 99
I've managed to sort it out. Here it is. Convert the date to timestamp then use function date and then pass it as a parameter.
$query->fields('fp', array('field_parution_value'));
$start = date('Y-m-d H:i:s', $timestamp_start);
$end = date('Y-m-d H:i:s', $timestamp_end);
$query->condition('fp.field_parution_value', $start, '>');
$query->condition('fp.field_parution_value', $end, '<=');
Upvotes: 1
Reputation: 1673
You should be able to do it using the BETWEEN operator. I'm not sure if Drupal supports DATE() closures as an arguments there - but you could use unix timestamp instead.
$test = db_select('node', 'n');
$test->fields('n');
$test->condition('created', array(strtotime('2015-01-01'), strtotime('2016-01-01')), 'BETWEEN');
$result = $test->execute();
Upvotes: 0