SQLSTATE[42883]: Undefined function: 7 ERROR: function date_format(date, unknown) does not exist

SQLSTATE[42883]: Undefined function: 7 ERROR: function date_format(date, unknown) does not exist

我创建这个是为了比较现在的时间与我的 table 行中的时间相同 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]: Undefined function: 7 ERROR: function date_format(date, unknown) does not exist

谁能帮我解决这个问题。

这可以简化。只需生成正确的日期时间格式(没有分钟)并使用 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();