Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

747
Vistas
PHP Carbon - How to compare a Datetime by minutes without seconds?

I have a query to get the bookings that were done on a specific date which is run by a cronjob (every minute). However, I cannot seem to query bookings rounded off to the nearest minute. How can this be achieved?

Booking::where('completed_at', now())->get();

This query only allows to compare

Y-m-d H:i:s

How can I compare

Y-m-d H:i

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

There's two solutions that come to mind:

Booking::whereBetween('completed_at', [now()->startOfMinute(), now()->endOfMinute() ])->get();

This gets everything within the same minute

Booking::where(\DB::raw("DATE_FORMAT(completed_at, '%Y-%m-%d %H:%i')"), now()->format('Y-m-d H:i'))->get();

This matches formatted dates.

I prefer the 1st approach because you don't need to worry about formatting issues and it can also make use of indexes.

over 4 years ago · Santiago Trujillo Denunciar

0

Asserting that now() will happen exactly every 60.00 seconds is wrong, cronjob run every 60 seconds +/- few ms, and PHP process before calling now() (and between 2 calls of now()) takes few µs to few ms depending on the operations.

So you should have a field like processed_by_the_cronjob which is false by default and you set it to true once your CRON job queried it.

Then you select:

Booking::where('completed_at', '<=', now())->andWhere('processed_by_the_cronjob', false)->get();

Else, once in a while you'll have booking that will get skipped.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda