【问题标题】:how to get the date from a datetime register in doctrine?如何从学说中的日期时间寄存器中获取日期?
【发布时间】:2013-09-30 00:40:57
【问题描述】:

我使用 codeigniter 和学说,这是我的 bd:

id fecha_publicacion
1  2013-03-23 14:52:06
2  2013-03-23 18:02:06
3  2013-03-23 00:00:00
..   ...

我想做的是从某个日期或某个日期范围内搜索那些出版物

问题是,如果我想选择所有日期为 2013-03-23 的字段,我的查询只返回第三个字段(时间为 00:00:00)

我该怎么做才能得到我想要的?我尝试了很多东西,但都没有成功

public function postsByDates($tipo,$fecha1,$fecha2=''){
    if($fecha2 == ''){
        $fecha2 = $fecha1;
    }

    $this->qb = $this->em->createQueryBuilder();
    $this->qb->select('p')
    ->from('models\Post', 'p')
    ->where(
        $this->qb->expr()->eq('p.tipo', '?1'),
        $this->qb->expr()->between('p.fecha_publicacion', '?2', '?3')
            )   
    ->setParameter(1, $tipo)
    ->setParameter(2, "$fecha1%")
    ->setParameter(3, "$fecha2%");

    $query = $this->qb->getQuery();
    $obj = $query->getResult();

    return $obj;
}

【问题讨论】:

    标签: doctrine-orm doctrine dql doctrine-query


    【解决方案1】:

    问题是您提供了日期时间/时间戳比较的日期。所以我猜它会将您提供的日期 (2013-03-23) 转换为时间戳 (2013-3-23 00:00:00)。

    快速修复:您可以设置两个日期,例如“2013-03-23 00:00:00”和“2013-03-23 23:59:59”。

    很好的解决方案:您的 DQL 原生查询调用 MySQL 的 date() 函数将您的时间戳字段 (fecha_publicacion) 转换为日期。之后,您将能够使用 between 功能。

    【讨论】:

      猜你喜欢
      • 2019-05-17
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 1970-01-01
      • 2023-03-22
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多