2 years ago

Select Number of Rows in Date Range via Query Builder?

Posted 2 years ago by xdega


So, I have this query which selects the number of rows in the past 30 days:

            SELECT COUNT( DATE_FORMAT( created_at, '%m/%d/%Y' ) )
            FROM    enc_encounters
            WHERE   created_at BETWEEN NOW() - INTERVAL 30 DAY AND NOW()";

I am trying to recreate this via query builder as such:

        $result = $this->select('DATE_FORMAT( created_at, \'%m/%d/%Y\' )')->whereBetween('created_at', ['NOW() - INTERVAL 30 DAY', 'NOW()'] )->count();

I am receiving a result of 0 (It is meant to return 3).

Any idea on where I am going wrong?

Please sign in or create an account to participate in this conversation.