5

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://i.stack.imgur.com/VMFVL.jpg

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.', []);
}
hhelderneves
  • 925
  • 8
  • 24
  • 1
    Carbon is UTC based therefore simply doing `Carbon::now()` will output the time in UTC format. You have to specify the timezone for an accurate reflection of the dateTime in your city or area. `Carbon::now('PST')` OR `Carbon::now('Continent/City')` like `(Carbon::now('America/Montreal')` for example – STA Jul 08 '20 at 16:03
  • @STA, thanks but already tried this and doesn't work. Even if I change to America. – hhelderneves Jul 08 '20 at 16:07
  • Can you tell me, which Laravel version you are using? – STA Jul 08 '20 at 16:07
  • @STA is correct here, under any circumstance Carbon::now('Europe/Lisbon'); Should give you the time in Lisbon. – Kurt Friars Jul 08 '20 at 16:08
  • Check updated question please. – hhelderneves Jul 08 '20 at 16:15
  • 1
    Do you get the same on `return Carbon::now()` and `dd(Carbon::now())`??? – STA Jul 08 '20 at 16:21
  • 1
    @Kurt seems like it a glitch on newer version, another same question here https://stackoverflow.com/questions/62758417/laravel-7-carbon-return-ever-time-as-utc/62758619?noredirect=1#comment110984137_62758619 – STA Jul 08 '20 at 16:22
  • 2
    This is a really odd issue. Hope someone can shine some light on it. Deserves some big upvotes here. – Kurt Friars Jul 08 '20 at 17:05
  • 1
    you can use date() function to help you with getting the correct time date_default_timezone_set('Europe/Lisbon'); echo date("YmdHis"); https://www.php.net/manual/en/function.date.php. – bhucho Jul 08 '20 at 18:58
  • @hmrneves Tengo el mismo problema, lo has llegado a solucionar? – Alexd2 Jul 09 '20 at 19:14
  • @Alexd2, no, I couldn't – hhelderneves Jul 09 '20 at 21:04
  • @hmrneves but this is a error of Carbon 2? – Alexd2 Jul 21 '20 at 09:17
  • @Alexd2, yes... – hhelderneves Jul 22 '20 at 14:00

1 Answers1

6

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');
Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49
  • thanks, but didn't work. I'm testing with American timezones hopping to get different values but I don't. – hhelderneves Jul 08 '20 at 16:09
  • This works for my previous version 5.x. but not in Laravel 7.x. I think there are a glich on Laravel 7. Dont idea about 6.x – STA Jul 08 '20 at 16:12