如何将datetime与DQL的CURRENT_DATE()进行比较

时间:2016-11-03 10:40:46

标签: symfony date datetime doctrine-orm dql

$em = $this->getEntityManager();
    $query = $em->createQuery(
        "SELECT b
        FROM AppBundle:Booking b
        AND b.checkin IS NULL
        AND b.bookingDate=CURRENT_DATE()"
    );

我想看今天的预订,但我也想在bookindDate预订时间。那么如何比较datetime bookingDate和CURRENT_DATE()?

3 个答案:

答案 0 :(得分:1)

您可以尝试以下DQL查询:

SELECT b
        FROM AppBundle:Booking b
        AND b.checkin IS NULL
        AND b.bookingDate BETWEEN CURRENT_DATE() AND CURRENT_DATE()

检查here BETWEEN doctrine2函数的文档

希望这个帮助

答案 1 :(得分:0)

你可以使用

$date = new DateTime();
$em = $this->getEntityManager();
    $query = $em->createQuery(
        "SELECT b
        FROM AppBundle:Booking b
        AND b.checkin IS NULL
        AND b.bookingDate=". $date->format('Y-m-d')
    );

答案 2 :(得分:0)

$em = $this->getEntityManager();
$query = $em->createQuery(
        "SELECT b
        FROM AppBundle:Booking b
        AND b.checkin IS NULL
        AND b.bookingDate >= CURRENT_DATE()
        AND b.bookingDate <= CURRENT_DATE()+1"
    );

这项工作对我来说太容易了;)