How to select lines in the last 2 hours in Doctrine

I am trying to fetch the last 2 hours of rows from a Symfony / Doctrine module. In normal MySQL it would be done like this: SELECT * FROM Posts WHERE

Date> SUBDATE( CURRENT_TIMESTAMP, INTERVAL 2 HOUR)

But Doctrine does not support these MySQL keywords like SUBDATE

and INTERVAL

. The Doctrine documentation provides only alternatives for intervals with days and months. But I need a watch. How to do it?

+3


source to share


1 answer


Use php DateTime

object - doctrine will handle this for you



$date = new \DateTime();
$date->modify('-2 hour');

$this
    ->createQueryBuilder('t')
    ->andWhere('t.date > :date')
    ->setParameter(':date', $date)
    ->getQuery()
    ->execute();

      

+15


source







All Articles