简体   繁体   English

比较日期时间与Doctrine之间的日期

[英]Compare dates between datetimes with Doctrine

I have a Syfmony2 app with a table which has a date field. 我有一个Syfmony2应用程序,其中包含一个具有日期字段的表。 This date field is a DateTime type. 此日期字段是DateTime类型。

I need to get all the entities which the same date as now. 我需要获得与现在相同的所有实体。

But if I do: 但如果我这样做:

$now = new \DateTime();
$data = $entityRepository->findByDate($now);

I get 0 results because Doctrine is comparing the DateTime object, and I need to compare only the year, month and day, not hours... only de Date object, not the DateTime. 我得到0结果,因为Doctrine正在比较DateTime对象,我需要只比较年,月和日,而不是小时...只有de Date对象,而不是DateTime。

Any Idea? 任何的想法? Thanks :D 感谢:D

I see this simple way: 我看到这个简单的方法:

$now = new \DateTime();

$data = $entityRepository->getByDate($now);

then in your repository 然后在你的存储库中

public function getByDate(\Datetime $date)
{
    $from = new \DateTime($date->format("Y-m-d")." 00:00:00");
    $to   = new \DateTime($date->format("Y-m-d")." 23:59:59");

    $qb = $this->createQueryBuilder("e");
    $qb
        ->andWhere('e.date BETWEEN :from AND :to')
        ->setParameter('from', $from )
        ->setParameter('to', $to)
    ;
    $result = $qb->getQuery()->getResult();

    return $result;
}

Method in repository 存储库中的方法

public function getDays(\DateTime $firstDateTime, \DateTime $lastDateTime)
{
    $qb = $this->getEntityManager()->createQueryBuilder()
        ->select('c')
        ->from('ProjectBundle:Calendar', 'c')
        ->where('c.date BETWEEN :firstDate AND :lastDate')
        ->setParameter('firstDate', $firstDateTime)
        ->setParameter('lastDate', $lastDateTime)
    ;

    $result = $qb->getQuery()->getResult();

    return $result;
}

And action 和行动

public function calendarAction()
{
    $currentMonthDateTime = new \DateTime();
    $firstDateTime = $currentMonthDateTime->modify('first day of this month');
    $currentMonthDateTime = new \DateTime();
    $lastDateTime = $currentMonthDateTime->modify('last day of this month');

    $days = $this->getDoctrine()
        ->getRepository('ProjectBundle:Calendar')
        ->getDays($firstDateTime, $lastDateTime);

    return ['days' => $days];
}

There is a difference between the date and datetime types in doctrine . 学说中datedatetime类型之间存在差异。

date : Type that maps a SQL DATETIME to a PHP DateTime object. date :将SQL DATETIME映射到PHP DateTime对象的类型。

datetime : Type that maps a SQL DATETIME/TIMESTAMP to a PHP DateTime object. datetime :将SQL DATETIME / TIMESTAMP映射到PHP DateTime对象的类型。

Make sure you have set the column type to date instead of datetime . 确保已将列类型设置为date而不是datetime

Alternatively - as a workaround - you could get the day from the original date1 and then search between a same-day date2 -> 00:00:00 and same-day date3 -> 23:59:59 using a custom repository method. 或者 - 作为解决方法 - 您可以从原始日期1获取日期,然后使用自定义存储库方法在同一天date2 - > 00:00:00和当天date3 - > 23:59:59之间进行搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM