2017-07-27 254 views
0

我正在构建一个查询,当daily_at时间和当前时间相等时,它将每分钟执行一次。变量daily_at是例如10:05:33的时间戳。比较时间戳和Carbon :: now() - > toTimeString()没有秒

我希望查询中的where子句在小时和分钟相同时通过,但不是第二个其他几乎不会执行。 where子句我现在有:

DB::table('restaurants') 
    ->where('schedule', '=', $schedule) 
    ->where('active', '=', 1) 
    ->whereDate('start_at', '<=', Carbon::today()) 
    ->where('daily_at', '=', Carbon::now()->toTimeString()) 
    ..... 

有人能帮助我调整where条款忽略秒?

回答

0

DB::table('restaurants') 
 
->where('schedule', '=', $schedule) 
 
->where('active', '=', 1) 
 
->whereDate('start_at', '<=', Carbon::today()) 
 
->where('daily_at', '=', Carbon::now('Y-m-d H:i')->toTimeString()) 
 
use this code

+0

这不是工作:'未知或坏的时区(Y-M-d H:我)'。而'daily_at'没有Y-m-d,所以它不会工作 –

1

尝试用:

DB::table('restaurants') 
->where('schedule', '=', $schedule) 
->where('active', '=', 1) 
->whereDate('start_at', '<=', Carbon::today()) 
->where('daily_at', '=', Carbon::now()->format('H:i')) 

另一种方式:

DB::table('restaurants') 
    ->where('schedule', '=', $schedule) 
    ->where('active', '=', 1) 
    ->whereDate('start_at', '<=', Carbon::today()) 
    ->where('daily_at', '=', Carbon::createFromTime(Carbon::now()->hour,Carbon::now()->minute,00)->format('H:i:s')) 

你可以传递一个额外的参数设置时区:

Carbon::createFromTime(Carbon::now()->hour,Carbon::now()->minute,00,'Example/Zone')->format('H:i:s') 
+0

太好了!现在我只需要从daily_at –

+0

得到小时和分钟不错!你需要什么?也许我可以帮助你。你会将我的答案设置为正确答案吗? – aaron0207

+0

现在'daily_at'的值是例如H:i:s。我需要失去'daily_at'的秒数,并且只有H:i才能进行比较! –

0

可以请你这样尝试:

whereRaw(DATE_FORMAT(daily_at, "%H:%i") = Carbon::now()->format('H:i')) 

所以最终的查询是,

DB::table('restaurants') 
->where('schedule', '=', $schedule) 
->where('active', '=', 1) 
->whereDate('start_at', '<=', Carbon::today()) 
->where('daily_at', '=', Carbon::now()->toTimeString()) 
->whereRaw(DATE_FORMAT(daily_at, "%H:%i") = Carbon::now()->format('H:i'))