[英]Laravel Eloquent Select Between current month and previous 3 months
i'm trying to build a query that will select all of the records in my DB between now (current month) and the previous 3 months.我正在尝试构建一个查询,它将选择现在(当前月份)和前 3 个月之间我的数据库中的所有记录。
My query somewhat works, but i want to ignore the day of the month.我的查询有些工作,但我想忽略一个月中的哪一天。 At the moment, it's selecting the last # of months to the current DAY as well but i want to ignore the current day and use the start and end of the months.
目前,它也在选择当前日期的最后几个月,但我想忽略当前日期并使用月份的开始和结束。
Here's my query:这是我的查询:
$dateS = Carbon::now()->subMonth(3);
$dateE = Carbon::now();
$TotalSpent = DB::table('orders')
->select('total_cost','placed_at')
->whereBetween('placed_at',[$dateS,$dateE])
->where(['deleted' => '0', 'delivery_address_id' => $DeliveryAddress->id])
->sum('total_cost');
Any help would be appreciated.任何帮助,将不胜感激。 It's really bugging me!
真是烦死我了!
This will do the trick I guess我猜这可以解决问题
$dateS = Carbon::now()->startOfMonth()->subMonth(3);
$dateE = Carbon::now()->startOfMonth();
$TotalSpent = DB::table('orders')
->select('total_cost','placed_at')
->whereBetween('placed_at',[$dateS,$dateE])
->where(['deleted' => '0', 'delivery_address_id' => $DeliveryAddress->id])
->sum('total_cost');
startOfMonth()
begins with 1st date of the month startOfMonth()
从当月的第一个日期开始
Use Carbon:使用碳:
$data = Data::where("created_at",">", Carbon::now()->subMonths(6))->get();
You need to import the namespace to use Carbon:您需要导入命名空间才能使用 Carbon:
use Carbon\Carbon;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.