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 WHERE
Date> 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?
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With