Alexd2
Alexd2

Reputation: 1114

Laravel 7- Carbon return ever time as UTC

This is a real stranger, when return the current time with Carbon, this is return ever time as UTC for me. I do not understand why if I config as Europe/Madrid everything.

Example:

return Carbon::now() 
The result is 2020-07-06 14:30:00

But is:

dd(Carbon::now())

The result is 2020-07-06 16:30:00

I not understand why.

My file app.php

'timezone' => 'Europe/Madrid',

And when load a model for example user

$user = User::find(1);

The created_at and updated_at return

2020-06-07 14:35:00

But in database the value saved is

2020-06-07 16:35:00

enter image description here

Upvotes: 1

Views: 1106

Answers (1)

STA
STA

Reputation: 34668

Carbon uses the default DateTime PHP object. You can set custom timezone as :

$date = Carbon::createFromFormat('Y-m-d H:i:s', $tz, 'Europe/Madrid');

In the AppServiceProvider.php you can add the php functionality to alter the timestamp for the whole project :

public function boot()
{
    Schema::defaultStringLength(191);
    date_default_timezone_set('Europe/Madrid');
}

Upvotes: 1

Related Questions