我在 Laravel 5.2 中收到以下错误
以下是查询:
$ansrow = DB::table('answers')
->where('user_id', $u_id)
->whereBetween('created_at', [
now()->format('Y-m-d H:00:00'),
now()->addHours(1)->format('Y-m-d H:00:00')
])
->first();
我收到以下错误:
Call to undefined function App\Http\Controllers\now()
最佳答案
您应该使用Carbon
,例如:
$ansrow = DB::table('answers')
->where('user_id', $u_id)
->whereBetween('created_at', [
\Carbon\Carbon::now()->format('Y-m-d H:00:00'),
\Carbon\Carbon::now()->addHours(1)->format('Y-m-d H:00:00')
])
->first();
并且不要忘记为 config/app.php
中的 'timezone'
键分配正确的值,否则您将获得 UTC
日期时间(UTC
是默认值)。
关于php - Laravel 中调用未定义函数 App\Http\Controllers\now(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57843994/