2

I have fields in my table for date, but they contain everything - day, year and month. Can I write a query to get only the records, which has month equal to the current month? I can do this:

$today = new \DateTime(); $month = $today->format('m'); $cat = $em->getRepository('EMBudgetTrackerBundle:Expense')->find(1); $ex_date = $cat->getDate(); 

and compare $month and $ex_date, but can I write some kind of query? Something like this:

public function getExpensesByMonth($month) { $q = $this->createQueryBuilder('e'); $q->select('e') ->where('e.date = :date') ->setParameter('date', $month); return $q->getQuery()->getResult(); } 

Thank you in advance! :)

1
  • How is your dates formatted in database table? What is the column type? Commented Jan 29, 2013 at 7:31

1 Answer 1

2

If you database column is in DateTime format you can use the DateTime object in your query. As far as I know you can only query for time ranges though.

public function getExpensesByMonth($beginning, $end) { $q = $this->createQueryBuilder('e'); $q->select('e') ->where('e.date > :beginning') ->andWhere('e.date < :end') ->setParameter('beginning', $beginning) ->setParameter('end', $end); return $q->getQuery()->getResult(); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.