Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select rows from last 2 hours in Doctrine

I'm trying to select the last 2 hours worth of rows from a Symfony/Doctrine unit. In normal MySQL, this would be done like this: SELECT * FROM Posts WHEREDate> SUBDATE( CURRENT_TIMESTAMP, INTERVAL 2 HOUR)

But Doctrine does not support these MySQL keywords like SUBDATE and INTERVAL. In Doctrine documentation, only alternatives for intervals with days and months are given. But I need hours. How do I do this?

like image 990
Harold Holsappel Avatar asked Oct 17 '14 09:10

Harold Holsappel


Video Answer


1 Answers

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();
like image 118
Tomasz Madeyski Avatar answered Oct 03 '22 10:10

Tomasz Madeyski