我想从模型 "Users" 中提取那些 created_at
日期距离今天已经超过30天的对象。
Carbon::now() ==> 我需要的是 ==> Carbon::now() - 30天
$users = Users::where('status_id', 'active')
->where( 'created_at', '<', Carbon::now())
->get();
如何实现这个目标?
我想从模型 "Users" 中提取那些 created_at
日期距离今天已经超过30天的对象。
Carbon::now() ==> 我需要的是 ==> Carbon::now() - 30天
$users = Users::where('status_id', 'active')
->where( 'created_at', '<', Carbon::now())
->get();
如何实现这个目标?
使用subDays()
方法:
$users = Users::where('status_id', 'active')
->where( 'created_at', '>', Carbon::now()->subDays(30))
->get();
$users = Users::where('status_id', 'active')
->whereDate( 'created_at', '>', now()->subDays(30))
->get();
你还可以使用 whereMonth / whereDay / whereYear / whereTime
$users = Users::where('status_id', 'active')
->where( 'created_at', '>', date('Y-m-d', strtotime("-30 days"))
->get();
date('Y-m-d 00:00:00', strtotime("-30 天"))
,使其更加简洁。 - tristanbaileycarbon::parse
一起使用。$users = Users::where('status_id', 'active')
->where( 'created_at', '>', Carbon::parse('Now -30 days'))
->get();
另外,对于“现在-30天”,也可以使用表达式“-30days”或“-720小时”。
“现在-30天”考虑了当前时间。如果您需要00:00的时间,请使用“今天-30天”。
->endOfDay()
或->hour(0)->minute(0)->second(0)
,或在日期设置中使用date('Y-m-d 00:00:00')
。 - tristanbailey