如何使用Dql获取2个日期之间的差异

sma*_*ech 6 dql symfony doctrine-orm

如何使用Dql获取2个日期之间的差异?

我尝试了以下代码,但是没有用.

$query =$this->_em->createQuery('select a,DATEDIFF(d,\'now\',a.date) from ABundle:Abonment a where d==1');
Run Code Online (Sandbox Code Playgroud)

我怎么解决这个问题?

Joã*_*ves 12

从这个来源http://docs.doctrine-project.org/en/2.1/reference/dql-doctrine-query-language.html#dql-functions:

DATE_DIFF(date1, date2) - 计算date1-date2之间的天数差异,在查询中,您的函数取3个参数.

在Doctrine2中,您必须使用以下适合您的函数之一而不是now()函数:

CURRENT_DATE() - Return the current date
CURRENT_TIME() - Returns the current time
CURRENT_TIMESTAMP() - Returns a timestamp of the current date and time.
Run Code Online (Sandbox Code Playgroud)

结论你的查询应该是这样的:

$query =$this->_em->createQuery('select a, DATE_DIFF(CURRENT_DATE(), a.date) as days from ABundle:Abonment a where days = 1');
Run Code Online (Sandbox Code Playgroud)