Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compare a date with current date using Doctrine 2?

Tags:

I have a database where an "expiry date" is stored. Now I want the current date with the stored expiry date compared. If the current date is before the expiry date, the data should be displayed like usual, but if the data is expired, I want it to be excluded.

I tried:

$query = $em->createQuery('SELECT d FROM test d WHERE d.expDate > :date')     ->setParameters('date', 'now()'); 

Help would be cool, or some tips if someone understands my problem.

like image 992
Adi Avatar asked May 14 '12 11:05

Adi


People also ask

How to compare 2 date PHP?

Comparing two dates in PHP is simple when both the dates are in the same format but the problem arises when both dates are in a different format. Method 1: If the given dates are in the same format then use a simple comparison operator to compare the dates. echo "$date1 is older than $date2" ; ?>

How to compare date from and to date in PHP?

we can analyze the dates by simple comparison operator if the given dates are in a similar format. <? php $date1 = "2018-11-24"; $date2 = "2019-03-26"; if ($date1 > $date2) echo "$date1 is latest than $date2"; else echo "$date1 is older than $date2"; ?>

How to match two time in PHP?

$d1 = new DateTime( $date1 ); $d2 = new DateTime( $date2 ); // Can use date/string just like strtotime. Now the final method is date_diff() class it is also a PHP built-in class. With this class or method, you can get a total of many objects of two dates.


1 Answers

There is another way:

$query = $em->createQuery('SELECT d FROM test d WHERE d.expDate > :today')     ->setParameter('today', new \DateTime())-> 
like image 115
fain182 Avatar answered Oct 06 '22 01:10

fain182