Reputation: 99
Basically, I want to query using a date range as follows:
$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);
}
and here is the entity for 'createDate':
/**
* @Column(type="datetime")
*/
private $createDate;
However, when I test using this date range: $createDateStart = 01-Jan-2014 to $createDateEnd = 31-Jan-2014. The entries from last year i.e. 2013 is displayed.
I'm guessing that I have to format $createDate as well (d-M-Y) but how do I do that from the query statement?
I'd appreciate your help.
Upvotes: 1
Views: 3502
Reputation: 530
$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
it solved.
Upvotes: 2