繁体   English   中英

在学说查询生成器中获取日期

[英]Get dates in doctrine query builder

美好的一天,

我想在我的学说查询生成器中获取“从现在开始两个月以上”和“从现在开始六个月以上”的日期。

我现在所拥有的是;

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

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

使用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 == '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'))
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM