3

我创建了这个来比较现在的时间与我的表行 executes_at 中的时间相同。

$dateNow = Carbon::now()->format('Y-m-d');
$hourNow = Carbon::now()->format('H');
$minuteNow = Carbon::now()->format('i');

$recordings = Recording::with('therapy')
        ->where(DB::raw("DATE_FORMAT(executes_at,'%Y-%m-%d')"), '=', $dateNow)
        ->where(DB::raw("DATE_FORMAT(executes_at,'%H')"), '=', $hourNow)
        ->where(DB::raw("DATE_FORMAT(executes_at,'%i')"), '=', $minuteNow)
        ->get();

它在 MySQL 中工作但因为现在我们使用 PostgreSQL 我有这个错误

SQLSTATE [42883]:未定义函数:7 错误:函数 date_format(日期,未知)不存在

有人可以帮我解决这个问题。

4

1 回答 1

2

这可以简化。只需生成正确的日期时间格式(不含分钟)并用于DATE_TRUNC()比较它:

$dateNowToMinute = Carbon::now()->format('Y-m-d H:i');
$recordings = Recording::with('therapy')
    ->where(DB::raw("DATE_TRUNC('minute', executes_at)"), '=', $dateNowToMinute)
    ->get();
于 2019-10-03T08:07:17.327 回答