【问题标题】:Symfony2 - How to fix the first day of the next month being included as the previous month?Symfony2 - 如何修复下个月的第一天被包含为上个月?
【发布时间】:2014-05-14 13:38:07
【问题描述】:

我的学说查询目前正在查询一个月内的所有帖子,以获取侧栏中的帖子存档。

问题是当我点击 2013 年 10 月时(这应该只显示 10 月的帖子)但是,11 月 1 日,下个月的 1 日也包括在内。

我怎样才能解决这个问题,使当月的第一天不包含在上个月的帖子中?

Doctrine2 查询

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

【问题讨论】:

    标签: php symfony doctrine-orm archive


    【解决方案1】:

    改变

    $toDate->modify("下个月第一天午夜");

    $toDate->modify("下个月午夜-1秒");

    【讨论】:

    • 完美!谢谢xiidea!
    猜你喜欢
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多