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

hjortur17's avatar

whereDate is past

Hi, how can I use Laravel to only get the records from the database where the date has passed? So for example, customers were supposed to pick up 9/9/2021 but didn't show up, so I want to get that record from the Database and display it in an overdue section. My dates are formatted like: day/month/year => 7/8/2021

This is what I have right now:

public function luggage(Request $request)
	{
		$today = date("j/n/Y");
		
		$bookingsOverdue = LuggageStorage::where('pickUpDate', '>', $today)->where('pickedUp', false)->get()->toJson();

		return view('dashboard.luggage', compact('bookingsOverdue'));
	}
0 likes
13 replies
kevinbui's avatar

I checked this thread. You might want to try the STR_TO_DATE method with the less than operator.

today = Carbon::today()->toDateString();

$bookingsOverdue = LuggageStorage::whereRaw("STR_TO_DATE(pickUpDate, '%d/%m/%Y') < {$today}")
    ->where('pickedUp', false)
    ->get();
MichalOravec's avatar
Level 75

You need to use opposite operator < instead of >

where('pickUpDate', '<', $today)
3 likes
hjortur17's avatar

@lemmon - But what are your thoughts? To use Carbon instead of doing $today?

hjortur17's avatar

@lemmon - When I try this, I get this error: Could not parse '27/8/2021': DateTime::__construct(): Failed to parse time string (27/8/2021) at position 0 (2): Unexpected character

lemmon's avatar

@hjortur17 Maybe start another thread and post some code, ie. how are you storing the date in the database, controller, form, etc.

Please or to participate in this conversation.