duanniying2342
2014-04-01 08:00
浏览 7
已采纳

doctrine2 with php Datetime month

I have some events stored in a database using doctrine and Symfony2, and those events have a two datetime parameters, start and end of the event. I need to know if those events are active during a certain month, but i cant find a way. I tried with :

$now = new \Datetime('now');
    $qb  = $this->createQueryBuilder('e');

    $qb->where('e.start <= :now AND e.end >= :now')
       ->setParameter('now', $now);

    return $qb->getQuery()
              ->getArrayResult();

but it limits to the day, and not to the month. Is there a way to check it ? Thanks a lot !

图片转代码服务由CSDN问答提供 功能建议

我使用doctrine和Symfony2将一些事件存储在数据库中,这些事件有两个日期时间参数,start和 活动结束。 我需要知道这些事件在某个月内是否有效,但我找不到办法。 我尝试过:

  $ now = new \ Datetime('now'); 
 $ qb = $ this-&gt; createQueryBuilder('e'); 
 \  n $ qb-&gt; where('e.start&lt; =:now AND e.end&gt; =:now')
  - &gt; setParameter('now',$ now); 
 
返回$ qb  - &gt; getQuery()
  - &gt; getArrayResult(); 
   
 
 

但它限制为当天,而不是月份。 有办法检查吗? 非常感谢!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongpu1908 2014-04-01 08:15
    已采纳

    You can use the following code

    $monthStart = new DateTime(date('Y-m-01') . " 00:00:00");
    $monthEnd = new DateTime(date('Y-m-t'). " 23:59:59");
    
    $qb  = $this->createQueryBuilder('e');
    
    $qb
       ->where('e.start <= :end AND e.end >= :start')
       ->setParameter('start', $monthStart)
       ->setParameter('end', $monthEnd);
    
    return $qb->getQuery()
              ->getArrayResult();
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题