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

akmadhwa's avatar

query where Date_format() issue

I try to get value from the db with date_format(created_at, '%Y-%m-%d %H:%i'), but it seem i got the wrong query and could not find any solution on the issue.

Anyone who knows how to retrieve created_at value with specific date_format? Can you help me on this?

0 likes
3 replies
deansatch's avatar

do your query as normal and set the format on your output:


$model = YourModel::all();

$formattedDate = \Carbon\Carbon::parse($model->created_at)->format('Y m d');//any format you watn

akmadhwa's avatar

I actually want to try to get data using where like

 ->where('customer_id',$customer_id)
                ->whereRaw('DATE_FORMAT(created_at, "%Y-%m-%d %H:%i") >='.$fromDate)
                ->whereRaw('DATE_FORMAT(created_at, "%Y-%m-%d %H:%i")'.$toDate);
        }

But it keeps on saying syntx error and return 400.

deansatch's avatar

your two where statements seem like they wouldn't work well together anyway - one or the other it seems. Here is one anyway which should help in doing the other

 ->where('customer_id',$customer_id)
		->whereRaw(" DATE_FORMAT(created_at,'%Y-%m-%d %H:%i') >= ? ", [$fromDate]);
        }

or maybe this...

 ->where('customer_id',$customer_id)
		->where(\DB::raw("(DATE_FORMAT(created_at,'%Y-%m-%d %H:%i'))"), ">=", $fromDate);
        }

Please or to participate in this conversation.