Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Filter results by date

Hello,

I have to make endpoint wich return data from my MySQL database that are created under 24h. I have table "cars" and column called "created" (timestamp) in it. How to filter data and display using ORM ?

Perhaps something like:

Cars::find([
    'created >= :date:',
    'bind' => [
        'date' => strtotime('-1 day')
    ]
]);

You can adjuest the strtotime() value for the period you need.



8.6k

Perhaps something like:

```php Cars::find([ 'created >= :date:', 'bind' => [ 'date' => strtotime('-1 day') ] ]); ```

You can adjuest the strtotime() value for the period you need.

Nice friend, thats what I was looking for. Thank you

I did it in this way before i read your posts :

            $date = date("Y-m-d");

            $cars = Cars::find(
                "created LIKE '$date%'"
            );


62.7k
Accepted
answer

Your query should work too. Just a thing to note:

Be 100% sure that $date is a safe value. If it comes from user input, better use binding.

Cars::find([
    'created LIKE :date:',
    'bind' => [
        'date' => $date .'%'
    ]
]);