Php Laravel-如何查询月份和年度日期?
我是拉雷维尔的新手,不知道该怎么做 我在数据库中将日期记录为Php Laravel-如何查询月份和年度日期?,php,laravel,eloquent,Php,Laravel,Eloquent,我是拉雷维尔的新手,不知道该怎么做 我在数据库中将日期记录为开始日在我的时间表中。 日期以以下格式保存:2019-10-03 现在我只有: $months = DB::table('times')->where('start_day', '=', 2019) 如何选择一年中每个月的数据?您需要将where()更改为whereYear()以按年份搜索 $months = DB::table('times')->whereYear('start_day', '=', 2019) 您需
开始日
在我的时间
表中。日期以以下格式保存:
2019-10-03
现在我只有:
$months = DB::table('times')->where('start_day', '=', 2019)
如何选择一年中每个月的数据?您需要将
where()
更改为whereYear()
以按年份搜索
$months = DB::table('times')->whereYear('start_day', '=', 2019)
您需要将
where()
更改为whereYear()
以按年份搜索
$months = DB::table('times')->whereYear('start_day', '=', 2019)
使用
whereYear
use Carbon\Carbon;
$months = DB::table('times')->whereYear('start_day', (string) Carbon::now()->year)->get();
如果只想选择月份,则
$months = DB::table('times')->select(DB::raw('MONTH(start_day) month')->whereYear('start_day', (string) Carbon::now()->year)->get();
使用
whereYear
use Carbon\Carbon;
$months = DB::table('times')->whereYear('start_day', (string) Carbon::now()->year)->get();
如果只想选择月份,则
$months = DB::table('times')->select(DB::raw('MONTH(start_day) month')->whereYear('start_day', (string) Carbon::now()->year)->get();
写
$month=array():
对于($i=1;$i whereyear('start_day','2019')->whereMonth('start_day',$i)->get();
}
或
$month=array():
对于($i=1;$i此处([['年(开始日)',=',2019],“'月(开始日)',=',(int)$i]])->get();
}
写入
$month = array():
for($i=1;$i<=12;$i++){
$month[] = DB::table('times')->whereYear('start_day','2019')->whereMonth('start_day',$i)->get();
}
$month=array():
对于($i=1;$i whereyear('start_day','2019')->whereMonth('start_day',$i)->get();
}
或
$month=array():
对于($i=1;$i此处([['年(开始日)',=',2019],“'月(开始日)',=',(int)$i]])->get();
}
您可以使用此格式尝试每月、每周、每年
$weeklyNumberOfSales = Sale::whereBetween('order_date', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])
->sum('qty');
$monthlyNumberOfSales = Sale::whereMonth('order_date',Carbon::now()->format('m'))
->sum('qty');
$yearlyNumberOfSales = Sale::whereYear('order_date',Carbon::now()->format('Y'))
->sum('qty');
型号名称销售。
“订单日期”和“数量”是字段名您可以使用此格式尝试每月、每周、每年
$weeklyNumberOfSales = Sale::whereBetween('order_date', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])
->sum('qty');
$monthlyNumberOfSales = Sale::whereMonth('order_date',Carbon::now()->format('m'))
->sum('qty');
$yearlyNumberOfSales = Sale::whereYear('order_date',Carbon::now()->format('Y'))
->sum('qty');
型号名称销售。
“订单日期”和“数量”是字段名