We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.



23.6k

Perhaps something like:

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%'"
            );


93.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 .'%'
    ]
]);