2

I'm receiving the following error in Laravel 5.2

The following is the query :

$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(); 

I receive the following error :

Call to undefined function App\Http\Controllers\now()
Abdallah Sakre
  • 915
  • 1
  • 9
  • 26
  • There is no `now()` in PHP. You can do like the way mentioned in the link https://stackoverflow.com/questions/1995562/now-function-in-php/1995566 – nice_dev Sep 08 '19 at 17:12

1 Answers1

4

You should use Carbon, e.g.:

$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(); 

And don't forget to assign the correct value to the 'timezone' key in config/app.php or you'll get UTC datetime (UTC is the default).

dparoli
  • 8,891
  • 1
  • 30
  • 38