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

Ab.net's avatar

laravel change date value format from db and compare it to current date

i am trying to compare my date value in the database to the current date but the date formats are different..how can i change the retrieved date from the db and compare it to the current date at the same time?? please help....

my current code

$event=Event::whereDate($time='start_date', date('d-m-y', strtotime($time)) >= date('d-m-y'))->orderBy('created_at','asc')->get();

This isnt returning any values from the database...

0 likes
8 replies
AbdallahSabri's avatar

$current_date = date("Y-m-d h:i:s", time());

$event=Event::where('start_date', '>=' , $current_date )->orderBy('created_at','asc')->get();

Ab.net's avatar

My start_date value is in a ('d M Y-H:i') format i have to change it to ('d-m-y) format in order to compare it... How can i do that?

RRStoyanov's avatar
$startDate = Carbon::createFromFormat('d M Y-H:i', $yourData);
$even = Event::where('start_date', '>=', $startData)->orderBy('created_at')->get();
Cronix's avatar

First, is start_date actually a column in the database or are you getting this from a datepicker or something? If it's a db field, what is the type of field? It sounds like you are using a varchar (instead of a datetime/timestamp/etc) since you are saying your date format is 'd M Y-H:i'. We need more info in order to give you accurate advise.

AbdallahSabri's avatar

@Ab.net you can change the date format in ($current_date) as you like

Example:

$current_date = date("d m Y-H:i", time());

Cronix's avatar

@Cronix its a varchar for the time being..

Then I think you have to cast it to a date in the query, otherwise it won't be able to compare dates (since it's a string in the database and not a date object).

You really should use a datetime or something for the field if you want to be able to use date operations/comparisons on them.

Please or to participate in this conversation.