问题
I have configured the timezone in config/app.php
to Europe/Lisbon
.
If I do a return date_default_timezone_get();
, It returns Europe/Lisbon
like I want.
So far so good!
When I do Carbon::now()
it returns for example 16 hour while the current time is 17h. Can someone explain why the daylight saving aren't considered?
I'm using Laravel 7.16, PHP 7.4
Regards
EDIT 1: Code image + Times | https://imgur.com/pfh6uij
EDIT 2: always done php artisan optimize:clear
to clear all caches when I change something in the config.
EDIT 3: created_at and updated_at on models are getting correct times.
EDIT 4: return Carbon::now()
returns bad hours. Doing dd(Carbon::now())
returns the correct values, with the Timezone configured in config/app.php
| dd() example -> date: 2020-07-08 17:25:28.935949 Europe/Lisbon (+01:00)
EDIT 5: Opened an issue at github - https://github.com/laravel/framework/issues/33475
EDIT 6: In my case, I used the php date() function to workaround the problem. Not the way I wanted but does the job...
if (date(now()) > $subscriber->token_expire_date)
{
// return not found response
return $this->response(false, 410, 'The token has expired.', []);
}
回答1:
Carbon uses the default DateTime PHP object, so use the date_default_timezone_set() function, for example: date_default_timezone_set('Europe/Lisbon');
or you define it AppServiceProvider
App/Providers/AppServiceProvider.php
public function boot()
{
date_default_timezone_set('Europe/Lisbon');
}
Or you can use setTimezone
of carbon method
echo Carbon::now()->setTimezone('Europe/Lisbon')->format('H:i');
来源:https://stackoverflow.com/questions/62798761/laravel-carbon-timezone