Filter on date from datetime in query builder (CakePHP 3)

982 Views Asked by At

I have a datetime field in my model. In a query I want to select all rows created on a specific day/date (the time doesn't matter). What is the simplest way of doing that in CakePHP 3.8 ?

1

There are 1 best solutions below

1
On BEST ANSWER

Per other answers, use of a custom function to cast the column with MySQL DATE() may slow your query down at scale. Besides, there's probably nothing simpler than plain arrays to build conditions:

$query = $this->YourTable->find()
    ->where([
        'date_field >=' => '2020-01-01 00:00:0', // Or pass a Time() object..
        'date_field <' => '2020-01-02 00:00:00',
    ]);

This kind of stuff is covered in the Query Builder docs.