我有一个表 mdl_forum_posts ,其中已创建字段
每周一(一周的第一天)都会运行一个cron,需要选择上一周创建的所有行(前一周创建的值)。
我试图这样做:
$currentDate = \Carbon\Carbon::now();
$agoDate = $currentDate->subDays($currentDate->dayOfWeek)->subWeek();
$postsNL = DB::table('mdl_forum_posts')->whereBetween('created', array($agoDate,$currentDate))->get();
但是这并没有返回任何行(它应该!)。
答案 0 :(得分:1)
请记住,当您对Carbon对象执行某些操作时,它将修改对象本身的实例,因此基本上在运行语句时
$agoDate = $currentDate->subDays($currentDate->dayOfWeek)->subWeek();
您还在修改$currentDate
。
以下代码应该可以解决问题:
$currentDate = \Carbon\Carbon::now();
$agoDate = $currentDate->copy()->subDays($currentDate->dayOfWeek)->subWeek()->setTime(23, 59, 59);
$postsNL = DB::table('mdl_forum_posts')
->whereBetween('created', array($agoDate->timestamp, $currentDate->timestamp))
->get();
copy
方法会对Carbon
个实例的副本进行所有修改,而不会对原始实例进行审核。
希望有所帮助
答案 1 :(得分:0)
为什么要这样? MySQL不只是将整数与日期进行比较。整数称为unix时间戳,您可以通过使用Carbon实例上的timestamp属性轻松地从OPC中获取unix时间戳:
;with session_islands as (
select id, session_id, value,
coalesce(sum(case when value <> 1 then 1 end)
over (partition by session_id order by id), 0) as grp
from @req_data
), islands_with_7 as (
select id, grp, value,
count(case when value = 7 then 1 end)
over (partition by session_id, grp) as cnt_7
from session_islands
)
select id
from islands_with_7
where cnt_7 = 0 and value = 1
由于Carbon扩展了DateTime,您还可以使用the rules of an invokevirtual
instruction方法。