How to use 'interval' in Doctrine2 Query Builder
@Kiran write only about DATE_ADD
, but you can also use DATE_SUB
$qb->andWhere("DATE(a2_.updatedAt) = DATE_SUB(CURRENT_DATE(), 6, 'day')");
It is equivalent of SQL:
DATE(a2_.updatedAt) = DATE_SUB(CURRENT_DATE, INTERVAL 6 DAY)
As far as I know, Interval is not ported in Doctrine. The workaround I found is to work directly on the DateTime I pass as a param (here, I'd like to use interval of 2 days, passed through Datetime):
public function findOngoingPublicEvents()
{
return $this->createQueryBuilder('e')
->where('e.isActive = 1')
->andWhere('e.isPublic = 1')
->andWhere('e.begin <= :begin')
->andWhere('e.end >= :end')
->orderBy('e.id', 'ASC')
->setParameter('begin', new \DateTime('+2 days'))
->setParameter('end', new \DateTime('-2 days'))
->getQuery()
->execute();
}
If you want to use INTERVAL (in Doctrine 2, DQL) on mysql comumn field, You can use as below,
$qb->andWhere("DATE_ADD(pv.myDAte,48,'hour') >= UTC_TIMESTAMP()");
It will print SQL as below,
...... DATE_ADD(p0_.appointment_date, INTERVAL 48 HOUR) >= UTC_TIMESTAMP() .....