Search code examples
symfony1propelsymfony-1.4

Symfony - search table by month, year for datetime fields


I am trying to create an search filter on a frontend module, that will filter by 2 fields, which are months and years

I have items in my db table, that have a datetime field.

I'd like to be able to create a search, where If I select January and 2010 from the 2 drop downs, then all items that have datetimes like:

2010-01-24 10:50:52

2010-01-25 10:50:52

Will be listed

I am using symfony 1.4 and Propel ORM

Thank you


Solution

  • Why dont you try to create 2 date and check if that date is between them, for example (2010-01-01 >= $date && 2010-01-31 <= $date ). If so, then your in range and all coincidences will appear.

    If you absolutely have to check for month and year i recommend using functions like YEAR(date) = $yourDate and Month(date) = $yourMonth, this functions should go along with a CUSTOM criteria and could look like:

    $criterias->addCriteria(TablePeer::DATE_ATTRIBUTE,"YEAR(". TablePeer::DATE_ATTRIBUTE .") = $year",Criteria::CUSTOM);
    $criterias->addCriteria(TablePeer::DATE_ATTRIBUTE,"MONTH(". TablePeer::DATE_ATTRIBUTE .") = $month",Criteria::CUSTOM);
    

    Here is a link to MysqlDateFunctions