I have an Orders table that has a field scan_date
in unix timecode. I am trying to get all Orders with a scan_date
WITHIN the current day. I was originally using whereBetween()
to grab orders within the last 24 hours of whenever it's run but I realized that's not doing exactly what I need.
->whereBetween('scan_date', [Carbon::now()->addDays(-1), Carbon::now()])
If an order has a scan_date
between 12:01AM and 11:59pm on the current day, I want to grab all those orders -- is this possible to query for?
Similar to what you were doing, with a small modification:
$today = \Carbon\Carbon::today();
$startOfDayTimestamp = $today->copy()->startOfDay()->timestamp;
$endOfDayTimestamp = $today->copy()->endOfDay()->timestamp;
->whereBetween('scan_date', [
$startOfDayTimestamp,
$endOfDayTimestamp
])
You can use whereDate
, on Laravel < 5.7 you should format the date to Y-m-d
before passing it:
->whereDate('scan_date', Carbon::now()->toDateString())
On Laravel >= 5.7 you can pass an instance of DateTimeInterface
directly:
->whereDate('scan_date', Carbon::now())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With