Obtenga solo registros creados hoy en laravel
¿Cómo uso el campo created_at
para obtener solo los registros que se crearon hoy y ningún otro día u hora?
Estaba pensando en un ->where('created_at', '>=', Carbon::now())
Pero no estoy seguro de que funcionaría.
6 answers
Para los usuarios de Laravel 5.6, solo puede hacer
$posts = Post::whereDate('created_at', Carbon::today())->get();
Feliz codificación
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-22 08:29:18
Use Mysql
la función predeterminada CURDATE
para obtener todos los registros del día.
$records = DB::table('users')->select(DB::raw('*'))
->whereRaw('Date(created_at) = CURDATE()')->get();
dd($record);
Nota y Actualización para 5.6
La diferencia entre Carbon::now
vs Carbon::today
es justo el tiempo.
E. g
La fecha impresa a través de Carbon::now
se verá como algo:
2018-06-26 07:39:10.804786 UTC (+00:00)
Mientras que con Carbon::today
:
2018-06-26 00:00:00.0 UTC (+00:00)
Para obtener los únicos registros creados hoy con now
se puede obtener como:
Post::whereDate('created_at', Carbon::now()->format('m/d/Y'))->get();
Mientras que con today
:
Post::whereDate('created_at', Carbon::today())->get();
UPDATE
A partir de laravel 5.3, tenemos la cláusula default where
whereDate / whereMonth / whereDay / whereYear
$users = User::whereDate('created_at', DB::raw('CURDATE()'))->get();
O con DB
fachada
$users = DB::table('users')->whereDate('created_at', DB::raw('CURDATE()'))->get();
Uso de la lista anterior donde las cláusulas
$users = User::whereMonth('created_at', date('m'))->get();
//or you could also just use $carbon = \Carbon\Carbon::now(); $carbon->month;
//select * from `users` where month(`created_at`) = "04"
$users = User::whereDay('created_at', date('d'))->get();
//or you could also just use $carbon = \Carbon\Carbon::now(); $carbon->day;
//select * from `users` where day(`created_at`) = "03"
$users = User::whereYear('created_at', date('Y'))->get();
//or you could also just use $carbon = \Carbon\Carbon::now(); $carbon->year;
//select * from `users` where year(`created_at`) = "2017"
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-26 07:45:00
Si estás usando carbono (y deberías, ¡es increíble!) con Laravel, simplemente puede hacer lo siguiente:
->where('created_at', '>=', Carbon::today())
Además de now()
y today()
, también puedes usar yesterday()
y tomorrow()
y luego usar lo siguiente:
-
startOfDay()
/endOfDay()
-
startOfWeek()
/endOfWeek()
-
startOfMonth()
/endOfMonth()
-
startOfYear()
/endOfYear()
-
startOfDecade()
/endOfDecade()
-
startOfCentury()
/endOfCentury()
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-06 12:48:47
Con carbono:
return $model->where('created_at', '>=', \Carbon::today()->toDateString());
Sin carbono:
return $model->where('created_at', '>=', date('Y-m-d').' 00:00:00');
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-20 03:05:44
Puede usar whereRaw('date(created_at) = curdate()')
si la zona horaria no es una preocupación o whereRaw('date(created_at) = ?', [Carbon::now()->format('Y-m-d')] )
de lo contrario.
Dado que el campo created_at
es una marca de tiempo, necesita obtener solo la parte de fecha e ignorar la parte de tiempo.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-21 16:50:50
$today = Carbon\Carbon::now()->format('Y-m-d').'%';
->where('created_at', 'like', $today);
Espero que te ayude
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-16 13:02:59