Search code examples
mysqlsqldrupal-7

Compare date and time to todays date and time


What would be the best way in MySQL to compare date and time on this format
dd.mm.yyyy min.sec (05.03.2019 10.30)
to the date and time right now?

The data is stored as type datetime in the database

Would i need to reformat my data, if so, how?


Solution

  • To only return true if the date has not passed

    This would do the trick directly as a query in my db:

    SELECT c.entity_id AS entity_id, 
    c.field_course_category_terms_tid AS field_terms_tid,
    e.entity_type AS entity_type, 
    e.field_end_date_value AS field_end_date_value
    FROM 
    field_data_field_terms c
    INNER JOIN field_data_field_end_date e ON c.entity_id = e.entity_id
    WHERE
    e.field_end_date_value > curdate()
    

    In my drupal query this did the trick:

    $query = db_select('table1', 'c');
    $query->innerJoin('table2', 'e', 'c.entity_id = e.entity_id');
    $query->fields('c',array('entity_id','field_terms_tid'))
      ->fields('e',array('entity_type', 'field_end_date_value'))
      ->where('e.field_end_date_value  now()');
      ->execute();
    $result = $result->fetchAll();