Search code examples
phpdrupaldrupal-7

drupal 7 date time compare between dates


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)', '<');

Solution

  • 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, '<=');