Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

marsuch's avatar

date range

Hi, I'm looking on the internet and nothing helps, I have a date in the database in the format Y-d-m H:i:s. Thus, for example, 2021-02-08 18:12:24. And I need to create a query condition so that I get records within 30 days back so far. The problem is probably that the time stamp also contains time.

The query looks like this:


public function ChechlOrder () {
        return DB::table('erp_obj')->where('o_prefix', '=', '_xrp')->count();
    }

Can anyone advise me how to do this?

0 likes
4 replies
viorel's avatar

@marsuch

You could also use Carbon since is already available within Laravel. It's a wrapper for DateTime class. The code should look like this:

$date = \Carbon\Carbon::now()->subDays(30)->startOfDay();
DB::table('erp_obj')->where('created_at', '>=', $date)->get();
porloscerros's avatar

There is also the whereDate clause, which will take only the date part of a timestamp. So you could do something similar to what viorel proposes:

DB::table('erp_obj')->whereDate('created_at', '>=', now()->subDays(30))->get();
marsuch's avatar

Thanks for the help, in the end it turned out to be too bad. I sat there for almost 15 hours yesterday. And so he didn't mean it to me anymore, I tried Carbon, of course. But they will also succeed originally. Because the error was in the prefix. When I had prefixes stored in the database in the form prefix_ and not _prefix.

Please or to participate in this conversation.