2017-07-17 100 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')) 
}