Php 在条令查询生成器中获取日期

Php 在条令查询生成器中获取日期,php,mysql,symfony,doctrine-orm,Php,Mysql,Symfony,Doctrine Orm,你好 我正在尝试在我的条令查询生成器中获取“从现在起2个月以上”和“从现在起6个月以下”的日期 我现在拥有的是, if ($type == 'older_than_two_months') { $qb->andWhere('i.createdAt < '); } if ($type == 'younger_than_six_months') { $qb->andWhere('i.createdAt > '); } $qb->orderBy('i.c

你好

我正在尝试在我的条令查询生成器中获取“从现在起2个月以上”和“从现在起6个月以下”的日期

我现在拥有的是,

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);
if($type==‘两个月以上’){
$qb->andWhere('i.createdAt<');
}
如果($type==‘小于六个月’){
$qb->andWhere('i.createdAt>');
}
$qb->orderBy('i.createdAt','DESC')
->setParameter('status',$status);

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

使用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'));
}
if($type==‘两个月以上’){
$qb->andWhere('i.createdAt<:olderThan')
->setParameter('olderThan',new\DateTime('-2个月');
}
如果($type==‘小于六个月’){
$qb->andWhere('i.createdAt>:youngerThan')
->setParameter('olderThan',new\DateTime('-6个月');
}
如果($type=='超过两个月'){
$qb->andWhere('f.dateAdded<:twoMonthsAgo')
$qb->setParameter(':twoMonthsAgo',(new\DateTime())->modify('-2months'))
}
如果($type==‘小于六个月’){
$qb->andWhere('f.dateAdded>:六个月前')
$qb->setParameter(':sixMonthsAgo',(new\DateTime())->modify('-6months'))
}
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'))
}