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

Apex's avatar
Level 1

change date time format

I am trying to fetch max(modified_date) whose dtype is datetime in mysql and then storing it in a cursor. When I try to fetch the response, it returns : [(datetime.datetime(2019, 9, 13, 9, 31, 5),)]

I am not able to change it into date time format as it is in the table from which I am trying to fetch. I want to convert my response in the below format, how should I do that? 2019-09-13-02.32.28 +0530

0 likes
2 replies
jlrdw's avatar

Mysql would not return 2019, 9, 13, 9, 31, 5 for a date or a datetime. So how is it actually stored, what type field.

Looks like you need to play around with php string functions to get that in a format that is correct, then and only then can you do something like:


$tmpdate = new \DateTime($yourfixedstringofthedate);

$mydate = $tmpdate->format('Y-m-d H:i:s');

or


$tmpdate = new \DateTime($yourfixedstringofthedate);

$mydate =  date_format($tmpdate,"Y/m/d H:i:s");  // whatever format
indersein's avatar

Try this one ... $user = \App\User::find(1);

$newDateFormat3 = \Carbon\Carbon::parse($user->created_at)->format('d/m/Y');

dd($newDateFormat3);

and carbon is a better exmple

Like This. {{ \Carbon\Carbon::parse($user->from_date)->format('d/m/Y')}}

Please or to participate in this conversation.