Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the difference between 2 dates with Dql

How can I get the difference between 2 dates with Dql?

I have tried the following code, but that didn't work.

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

How can I solve this?

like image 679
smarttech Avatar asked Jun 02 '13 00:06

smarttech


People also ask

How can I compare two dates in SQL?

This can be easily done using equals to(=), less than(<), and greater than(>) operators. In SQL, the date value has DATE datatype which accepts date in 'yyyy-mm-dd' format. To compare two dates, we will declare two dates and compare them using the IF-ELSE statement.

What is a datediff () function?

You can use the DateDiff function to determine how many specified time intervals exist between two dates. For example, you might use DateDiff to calculate the number of days between two dates, or the number of weeks between today and the end of the year.

How can I calculate days between two dates in SQL?

The DATEDIFF() function returns the difference between two dates.


1 Answers

From this source http://docs.doctrine-project.org/en/2.1/reference/dql-doctrine-query-language.html#dql-functions:

DATE_DIFF(date1, date2) - Calculate the difference in days between date1-date2, and in your query your function is taken 3 parameters.

In Doctrine2 you have to use one of the following functions that fits better on you instead of now() function:

CURRENT_DATE() - Return the current date
CURRENT_TIME() - Returns the current time
CURRENT_TIMESTAMP() - Returns a timestamp of the current date and time.

On conclusion your query should by something like:

$query =$this->_em->createQuery('select a, DATE_DIFF(CURRENT_DATE(), a.date) as days from ABundle:Abonment a where days = 1');
like image 192
João Alves Avatar answered Sep 28 '22 14:09

João Alves