7

I try to compare the start date of an event with the current date in order to only display the next events. This is my try in the eventRepository:

public function findNext() {
    $query = $this->createQuery();
    $query->matching(
        $query->greaterThanOrEqual('datum_beginn', new \DateTime('midnight'))
    );
    return $query->execute();
}

But the result is not as expected. This is the resulting query:

SELECT events.* FROM events WHERE events.datum_beginn >= 1413669600 AND ...

As you can see the DateTime is converted to a timestamp. How can I either use MySQL NOW() in the query OR use DateTime properly?

lorenz
  • 4,538
  • 1
  • 27
  • 45
qualle
  • 1,347
  • 3
  • 14
  • 29

2 Answers2

17

Use string value of the date:

public function findNext() {
    $query = $this->createQuery();
    $date = new \DateTime('midnight');
    $query->matching(
        $query->greaterThanOrEqual('datum_beginn', $date->format('Y-m-d H:i:s'))
    );

    return $query->execute();
}
biesior
  • 55,576
  • 10
  • 125
  • 182
  • 4
    This did not work for me (Extbase 6.2). I had to use $date->getTimestamp() instead. – Martin Jun 03 '15 at 08:47
  • 1
    @biesior: You're awesome. I got almost everything from your answers. Cheers! :) – Hoja Aug 18 '15 at 13:13
  • 4
    Actually that depends on that in which format is stored in DB it may be unix timestamp or MySQL timestamp... OP had problems, cause he uses second format so comparing it with unix timestamp (`$date->getTimestamp()`) doesn't make sense – biesior Mar 15 '16 at 10:39
2

In 9 LTS it is possible to compare against DateTime directly:

$query->greaterThanOrEqual('datum_beginn', new \DateTime)
JKB
  • 499
  • 2
  • 13