美好的一天,

我对如何在仅评估日期部分而忽略时间部分的laravel的whereBetween函数上执行查询感到困惑。

$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');


这里的问题是'2015-05-01'中的所有交易都没有包含在内,因为它的时间不是00:00:00,这就是为什么要在'2015-05-01'上获得交易,我必须添加一天到我的$endDate变量。

最佳答案

尝试这个 :

$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');


此代码计算从2015-04-31 00:00:01 am到2015-05-01 00:00:01 am的所有交易
如果要包括2015年5月1日包括的所有交易,请使用此代码作为结束时间

$endDate = Carbon::createFromFormat('Y-m-d H:i:s','2015-05-1' . '23:59:59');

关于php - 如何仅在laravel whereBetween()上查询日期部分,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30396629/

10-11 03:34
查看更多