繁体   English   中英

在mysql / symfony2中比较日期

[英]Comparing dates in mysql / symfony2

我一直在寻找,还有一些相关的问题,但是没有人给我我需要的答案。 我有一个带有日期字段的实体,我需要选择从现在起7天以上的实体:

$query = $repository->createQueryBuilder('rf')
                    ->where('rf.sendDate >='.new \DateTime('-7 days'))
                    ->getQuery();

我收到此错误:

Catchable Fatal Error: Object of class DateTime could not be converted to string

我想知道的是,当在实体rf.sendDate定义为DateTime对象时,为什么假设rf.sendDate是字符串? 我该如何比较?

任何解释都非常感谢。

您应该为此使用参数:

    $query = $repository->createQueryBuilder('rf')
            ->where('rf.sendDate >= :ts')
                ->setParameter('ts', new \DateTime('-7 days'))
            ->getQuery();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM