Symfony2 - How to fix the first day of the next month being included as the previous month?

StackOverflow https://stackoverflow.com/questions/22825545

Вопрос

My doctrine query is currently querying all posts within a month for a posts archive in the sidebar.

The problem is when I click on October 2013 (this should only show me posts for October) however, Nov 1st, the 1st of the next month is being included as well.

How can I fix this so the first of the month is not included as a post for the previous month?

Doctrine2 query

public function getPostsByMonth($year, $month)
{
    // Query for blog posts in each month
    $date = new \DateTime("{$year}-{$month}-01");
    $toDate = clone $date;
    $toDate->modify("first day next month midnight");

    $qb = $this->createQueryBuilder('b')
        ->where('b.created BETWEEN :start AND :end')
        ->setParameter('start', $date)
        ->setParameter('end', $toDate)
    ;

    return $qb->getQuery()->getResult();
}
Это было полезно?

Решение

Change

$toDate->modify("first day next month midnight");

to

$toDate->modify("next month midnight -1 second");

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top