0

using php 5.5.9 . I have Tranche Entity, and it has a date attribute. What I want is to get number of rows matching a criteria; having date gt/lt current date ,so I created a twig extension, this is my code: following this this

public function e()
{
    $criteria = new \Doctrine\Common\Collections\Criteria();
    $date=date('Y-m-d');
    $criteria->where($criteria->expr()->lt('date',$date));
    $result=$this->em->getRepository('OCUserBundle:Tranche')->matching($criteria);
    return count($result);
}

My problem is that I can't compare dates no matter what I tried.

This is the error:

DateType.php line 53: Error: Call to a member function format() on a non-object

I've searched a lot and tried many suggestions, not working and I'm stuck. What's happening and thank you in advance.

Community
  • 1
  • 1
Abdelaziz Dabebi
  • 1,624
  • 1
  • 16
  • 21

2 Answers2

2
public function e() {
 $criteria = new \Doctrine\Common\Collections\Criteria();
 $d=date('Y-m-d'); // i think your error is here
 $timestamp = strtotime($d); //here
 $date=date('Y-m-d',$timestamp); //and here

 $d = //place here where the date is coming from
 $date = date('Y-m-d', strtotime($d)); //formatter of date
 $criteria->where($criteria->expr()->lt('date',$date));
 $result=$this->em->getRepository('OCUserBundle:Tranche')->matching($criteria);
 return count($result);
}
aldrin27
  • 3,407
  • 3
  • 29
  • 43
1

Solved as @Qoop said. It should be :

$criteria->where($criteria->expr()->lt('date', new \DateTime())); 
Abdelaziz Dabebi
  • 1,624
  • 1
  • 16
  • 21