0

Good day,

Im confuse on how can I perform a query on laravel's whereBetween function in evaluating only the Date part and disregarding the time part.

$startDate = '2015-04-31';
$endDate = '2015-05-01';
$totalDebit = DB::table('x_general_transactions_details')
            ->whereBetween('date_at', array($startDate,$endDate))
            ->where('account_xid',$account->xid)
            ->sum('debit');

the problem here is that all the transaction in '2015-05-01' is not being included because its time is not 00:00:00 that's why in order to get the transactions on '2015-05-01' i have to add one day to my $endDate variable.

3 Answers 3

1

try this :

$startDate = Carbon::createFromFormat('Y-m-d','2015-04-31');
$endDate = Carbon::createFromFormat('Y-m-d','2015-05-1');
$totalDebit = DB::table('x_general_transactions_details')
            ->whereBetween('date_at', array($startDate,$endDate))
            ->where('account_xid',$account->xid)
            ->sum('debit');

this code calculate all transactions from 2015-04-31 00:00:01 Am to 2015-05-01 00:00:01 am if you want include all transaction on 2015-05-01 included use this code for end time

$endDate = Carbon::createFromFormat('Y-m-d H:i:s','2015-05-1' . '23:59:59');
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer @user1873569.. I guess this is the easiest way to solve the problem :)
1

Add a time to your dates, it will save you a day.

$startDate = '2015-04-31 00:00:00';
$endDate   = '2015-05-01 23:59:59';

Comments

1

I personally would just cast the database field to a date:

$totalDebit = DB::table('x_general_transactions_details')

        ->whereBetween(DB::raw('DATE(date_at)'), array($startDate,$endDate))
                       ^^^^^^^^^^^^^^^^^^^^^^^^

        ->where('account_xid',$account->xid)
        ->sum('debit');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.