Wrong timestamp in laravel

Viewed 901

I am getting the wrong timestamp format/timezone in return from MySQL. In MySQL the time is accurate and correct but when I select it from MySQL I get it like this

2020-03-22T19:00:04.000000Z

However, I have this in my database table

2020-03-22 07:00:02

The query which I am using to fetch results from db is:

Stats::select('created_at','total_cases','recovered','critical','deaths','islamabad'
,'punjab','sindh','kp','balochistan','ajk','gb')->whereDate('created_at', $date)->get();
2 Answers

You have to apply date_format() to print the date returned from the database according to the format you want. Here is an example:

$date=date_create("2020-03-22T19:00:04.000000Z");
echo date_format($date,"Y/m/d H:i:s");
#output 2020/03/22 19:00:04

You can format your date using DATE_FORMAT function of mysql as below:

$data  =  Stats::select(DB::raw('DATE_FORMAT(created_at, "%Y-%m-%d %H:%i:%s") as formatted_dob'), 'total_cases','recovered','critical','deaths','islamabad'
    ,'punjab','sindh','kp','balochistan','ajk','gb')
    ->whereDate('created_at', $date)
    ->get();

Reference:

MySQL -> date and time function -> date_format

Related