Symfony 2 PHP比较日期问题


Symfony 2 PHP comparing dates issues

使用PHP 5.5.9。我有Tranche Entity,它有一个日期属性。我想要的是得到符合条件的行数;有日期gt/lt当前日期,所以我创建了一个分支扩展,这是我的代码:下面这个

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);
}

我的问题是,无论我怎么努力,我都无法比较日期。

错误:

datettype .php第53行:错误:在非对象上调用成员函数format()

我搜索了很多,尝试了很多建议,但都不起作用,我被卡住了。发生了什么事,提前谢谢你。

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);
}

按@Qoop说的解决。应该是:

$criteria->where($criteria->expr()->lt('date', new 'DateTime()));