2017-07-17 98 views
1

好日子,获取学说查询生成器日期

我想“从现在起2个月以上”得到的日期,在我的学说查询生成器“从现在起未满六个月。

我现在拥有的是;

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); 

我只需要添加额外的参数吗?但我不知道如何得到几个月前的日期。

回答

2

其实很简单用PHP的DateTime的:

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')); 
} 
0
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')) 
}