Search code examples
phpdatetimesymfonydoctrine-ormdql

How to filter a query result based on a date time column in symfony2 DQL


I have a query similar to this :

$em->createQuery(
    'SELECT t FROM myTestBundle:User t WHERE t.userId = :username AND
    t.tokenCreatedtime > (NOW() - INTERVAL 800 MINUTE)'
);

(NOW() - INTERVAL 800 MINUTE) is not working in DQL. Is there any other way that i can achieve this?


Solution

  • I would recommend you to create a placeholder and pass the DateTime object

    $q = $em->createQuery(
        'SELECT t FROM myTestBundle:User t WHERE t.userId = :username AND
        t.tokenCreatedtime > :createdTime '
    );
    $q->setParameter(':createdTime',new \DateTime("-800 minute")
    

    Also you might want to create the \DateTime Object as new \DateTime("-800 minute")