如何在querybuilder中比较两个日期



我正在尝试创建一个搜索表单,我需要将所选日期和数据库中的其他日期进行比较。所选日期的格式为(dd/mm/yyyy),而数据库中保存的日期的格式则为[mm-dd-yyyy]

$qb = $this->createQueryBuilder('t')
        ->leftJoin('t.image', 'i')->addSelect('i')
        ->leftJoin('t.country', 'country')->addSelect('country')
        ->leftJoin('t.destination', 'destination')->addSelect('destination')
        ->leftJoin('t.airport', 'airport')->addSelect('airport')
        ->Where('t.agence = :agence')
        ->setParameter('agence', $agence);
   //.........
    if (!empty($departureDate)) {
        $qb->andWhere('t.departureDate <= :departureDate')
            ->setParameter('departureDate', new Datetime($departureDate));
    }
return $qb;

如果所选日期高于13,则此查询显示此错误

DateTime::__construct() [<a href='datetime.--construct'>datetime.--construct</a>]: Failed 
to parse time string (13/02/2015) at position 0 (1): Unexpected character

如果小于13,则查询不会给我正确的结果

您有错误的国际化配置:您的日期被解析为mm/dd/yyyy。你需要改变这种行为:

$departureDate = new DateTime::createFromFormat('dd/mm/YY', $departureDate)

检查此项$departureDate = new DateTime::createFromFormat('d/m/Y', $departureDate)->format('m-d-Y');

我通过设置这样的查询来解决它:

if (!empty($departureDate)) {
        $date = new DateTime;
        $qb->andWhere('t.departureDate <= :departureDate')
            ->setParameter('departureDate', $date->createFromFormat('d/m/Y',$departureDate));
    }

相关内容

  • 没有找到相关文章

最新更新