简体   繁体   English

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

[英]How to query the Date part only on laravel whereBetween()

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. 我对如何在仅评估日期部分而忽略时间部分的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');

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

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

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 此代码计算从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');

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM