laravel carbon wherebetween 日期无法正常工作
laravel carbon wherebetween date not working properly
我想要的是能够从周数中获取记录。但是当我使用第一个查询时,没有任何返回。第二个查询 returns 同一周的记录,但查询中有碳,然后就可以了。
$date = Carbon::now();
$date->setISODate(2020,20);
return Timesheet::where('user_id', Auth::user()->id)->whereBetween('date', array($date->startOfWeek(), $date->endOfWeek()))->get();
输出:
[]
return Timesheet::where('user_id', Auth::user()->id)->whereBetween('date', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])->get();
输出:
[{"id":3,"user_id":1,"project_id":1,"start_time":"2020-05-17 10:58:25","end_time":"2020-05-17 13:58:25","date":"2020-05-16","description":"text","created_at":"2020-05-17T14:58:25.000000Z","updated_at":"2020-05-17T14:58:25.000000Z"}]
我做错了什么?
当然会是[].
回到你的第一个之间:
Timesheet::where('user_id', Auth::user()->id)
->whereBetween('date', array($date->startOfWeek(), $date->endOfWeek()))->get();
请考虑一下:
$date->startOfWeek();
这一行相当于:
$date=$date->startOfWeek();
so: 最后的变量 '$date' 将保存一个值, 是周末,whereBetween 将在相同的值之间!
要避免这种情况,您可以使用:
Timesheet::where('user_id', Auth::user()->id)->whereBetween('date', array(
Carbon::now()->startOfWeek(),
Carbon::now()->endOfWeek()))
->get();
或:
$weekStart=Carbon::now()->startOfWeek();
$weekEnd=Carbon::now()->endOfWeek();
Timesheet::where('user_id', Auth::user()->id)->whereBetween('date', array(
$weekStart,
$weekEnd))
->get();
请注意,如果您想将日期添加到 Carbon 类型的变量而不更改变量值,您可以使用 Carbon::make() 方法:
$value=Carbon::now();
$afterThreeDaysFromValue=Carbon::make($value)->addDays(3);
我想要的是能够从周数中获取记录。但是当我使用第一个查询时,没有任何返回。第二个查询 returns 同一周的记录,但查询中有碳,然后就可以了。
$date = Carbon::now();
$date->setISODate(2020,20);
return Timesheet::where('user_id', Auth::user()->id)->whereBetween('date', array($date->startOfWeek(), $date->endOfWeek()))->get();
输出:
[]
return Timesheet::where('user_id', Auth::user()->id)->whereBetween('date', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])->get();
输出:
[{"id":3,"user_id":1,"project_id":1,"start_time":"2020-05-17 10:58:25","end_time":"2020-05-17 13:58:25","date":"2020-05-16","description":"text","created_at":"2020-05-17T14:58:25.000000Z","updated_at":"2020-05-17T14:58:25.000000Z"}]
我做错了什么?
当然会是[].
回到你的第一个之间:
Timesheet::where('user_id', Auth::user()->id)
->whereBetween('date', array($date->startOfWeek(), $date->endOfWeek()))->get();
请考虑一下:
$date->startOfWeek();
这一行相当于:
$date=$date->startOfWeek();
so: 最后的变量 '$date' 将保存一个值, 是周末,whereBetween 将在相同的值之间!
要避免这种情况,您可以使用:
Timesheet::where('user_id', Auth::user()->id)->whereBetween('date', array(
Carbon::now()->startOfWeek(),
Carbon::now()->endOfWeek()))
->get();
或:
$weekStart=Carbon::now()->startOfWeek();
$weekEnd=Carbon::now()->endOfWeek();
Timesheet::where('user_id', Auth::user()->id)->whereBetween('date', array(
$weekStart,
$weekEnd))
->get();
请注意,如果您想将日期添加到 Carbon 类型的变量而不更改变量值,您可以使用 Carbon::make() 方法:
$value=Carbon::now();
$afterThreeDaysFromValue=Carbon::make($value)->addDays(3);