Doctrine查询:如何查询日期范围?

时间:2014-01-22 07:07:19

标签: doctrine-orm doctrine-query

基本上,我想使用日期范围进行查询,如下所示:

$qb = $this->createQueryBuilder("c");

if ($createDateStart) {
        $createDateStart = $createDateStart->format('d-M-Y');
        $qb->where("c.createDate >= :createDateStart")->setParameter(
                "createDateStart", $createDateStart);
}

if ($createDateStart && $createDateEnd) {
        $createDateEnd = $createDateEnd->format('d-M-Y');
        $qb->andWhere("c.createDate <= :createDateEnd")->setParameter(
                "createDateEnd", $createDateEnd);
} else 
        if ($createDateEnd) {
            $createDateEnd = $createDateEnd->format('d-M-Y');
            $qb->where("c.createDate <= :createDateEnd")->setParameter(
                    "createDateEnd", $createDateEnd);
        }

这里是'createDate'的实体:

/**
 * @Column(type="datetime")
 */
private $createDate;

但是,当我使用此日期范围进行测试时:$ createDateStart = 01-Jan-2014 to $ createDateEnd = 2014年1月31日。显示去年即2013年的条目。

我猜我也必须格式化$ createDate(d-M-Y)但是如何从查询语句中做到这一点?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

$qb->where('e.fecha BETWEEN :monday AND :sunday')
   ->setParameter('monday', $monday->format('Y-m-d'))
   ->setParameter('sunday', $sunday->format('Y-m-d'));

Select entries between dates in doctrine 2

它解决了。