Reputation:
Good day,
I'm trying to get the dates 'older than 2 months from now' and 'younger than six months from now' in my doctrine query builder.
What I currently have is;
if ($type == 'older_than_two_months') {
$qb->andWhere('i.createdAt < ');
}
if ($type == 'younger_than_six_months') {
$qb->andWhere('i.createdAt > ');
}
$qb->orderBy('i.createdAt', 'DESC')
->setParameter('status', $status);
Do I just have to add an extra parameter? But I don't know how to get the date of a few months ago.
Upvotes: 2
Views: 2457
Reputation: 121
if ($type == 'older_than_two_months') {
$qb->andWhere('f.dateAdded < :twoMonthsAgo')
$qb->setParameter(':twoMonthsAgo', (new \DateTime())->modify('-2months'))
}
if ($type == 'younger_than_six_months') {
$qb->andWhere('f.dateAdded > :sixMonthsAgo')
$qb->setParameter(':sixMonthsAgo', (new \DateTime())->modify('-6months'))
}
Upvotes: 0
Reputation: 17566
Actually very simple with the PHP DateTime's:
if ($type == 'older_than_two_months') {
$qb->andWhere('i.createdAt < :olderThan')
->setParameter('olderThan', new \DateTime('-2 months'));
}
if ($type == 'younger_than_six_months') {
$qb->andWhere('i.createdAt > :youngerThan')
->setParameter('olderThan', new \DateTime('-6 months'));
}
Upvotes: 3