Search code examples
phpdoctrine-ormdql

Doctrine 2. How to calculate difference between two datetime fields in querybuilder


I have to following tables:

Registration
    id int
    created datetime

Activity
    id int
    startdate datetime

Now I want to add this condition to my SQL query:

(Registration.created + 7 days) <= Activity.startdate

How can I do this in Doctrine 2? I prefer a database independent solution.


Solution

  • DATE_SUB was the solution. I used it with doctrine 2.1.

    $EntityManager->createQueryBuilder()->expr()->gte("r.created", "DATE_SUB(a.startDate, 7, 'day')")