如何仅在laravel whereBetween()上查询Date部分

时间:2015-05-22 12:22:03

标签: php mysql laravel-4

美好的一天,

我很困惑如何在laravel的whereBetween函数中仅评估Date部分并忽略时间部分时执行查询。

$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变量中添加一天。

3 个答案:

答案 0 :(得分:1)

为您的日期添加时间,这将为您节省一天的时间。

CommandAction

答案 1 :(得分:1)

试试这个:

$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-05-01中的所有交易,请使用此代码作为结束时间

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

答案 2 :(得分:1)

我个人只是将数据库字段转换为日期:

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

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

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