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.
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" ; ?>
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"; ?>
$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.
There is another way:
$query = $em->createQuery('SELECT d FROM test d WHERE d.expDate > :today') ->setParameter('today', new \DateTime())->
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With