Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pickab00's avatar

Extend date to 3 more hours where post = today's post

I know the title is a bit confusing. I myself am trying to make sense of it. Basically I am using Carbon to get the posts for today and this is how it looks like.

$array = Reservation::whereDate('reservation_made_on', Carbon::today())->get();

So with this, I am getting the reservations made for today (if any). Now that means that the reservations will not be visible after 12:00 AM/24:00. But I want it to be visible till 03:00 AM. So basically its tomorrow and no longer today. How can I achieve this.

0 likes
5 replies
bobbybouwmann's avatar
Level 88

It's better to do a whereBetween here on the date column. So something like this

$now = Carbon::now();
$start = Carbon::now()->startOfDay();
$end = Carbon::now()->endOfDay();

// We need this to make sure you see the results from the day before until 3AM
// After 3AM it will show the rest of the day.
if ($now->hour < 3) {
    $start->subDay();
    $end->subDay();
}

$reservations = Reservation::whereBetween('reservation_made_on', [$start, $end])->get();

Note that you can also update $start and $end to be a specific date range. Now it's always from 00:00 to 24:00 but I can image you might want it to be 03:00 till 03:00 the next day

$start = Carbon::now()->startOfDay()->addHours(3);
$end = Carbon::now()->endOfDay()->addHours(3);

Let me know if this works for you!

1 like
pickab00's avatar

@BOBBYBOUWMANN -

$start = Carbon::now()->startOfDay()->addHours(3);
$end = Carbon::now()->endOfDay()->addHours(3);

This bit is exactly what I needed. So now I can do this:

$start = Carbon::now()->startOfDay();
$end = Carbon::now()->endOfDay()->addHours(3);

$reservations = Reservation::whereBetween('reservation_made_on', [$start, $end])->get();

So now it will show all the reservations which were made for today and keep them till tomorrow 3AM (bacuse of 3 hours added). But my question is this. Will it collapse with the todays data? I mean if it is 3AM, it will show tomorrows data and todays data right? So is it better to do

$start = Carbon::now()->startOfDay()->addHours(3);
$end = Carbon::now()->endOfDay()->addHours(3);

So that it displays from 3AM to 3AM? So it will not show 2 days data at once (2 days being today and tomorrows) Hope I am making sense here

Please or to participate in this conversation.