Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

740
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!