Christian
Christian

Reputation: 1

PHP/Laravel - Carbon and DateTime returning wrong date/time

I have the folowing Problem. I am saving a Carbon or DateTime object in a Variable.

    $now = Carbon::now('Europe/Berlin');

If I am using this command the first time. Everything works fine. But on the one Day later it returns the last Date and Time generated on the Day before.

For example:

At this moment it is the correct Date and Time would be: 2018-12-14 13.48:00 but

    dd($now);

returns

Carbon @1544733487 {#555 ▼
  date: 2018-12-13 21:38:07.319843 Europe/Berlin (+01:00)
}

I am using Laravel 5.7 and i have tested it on my mac and on my shared hosting provider.

I don't have any idea what i am doing wrong. I would be happy if you can help me.

Regards Christian

Upvotes: 0

Views: 4851

Answers (2)

kapitan
kapitan

Reputation: 2222

On laravel, open you config/app.php and look for the "timezone" entry. enter your own timezone on it.

'timezone' => 'your_timezone_here'

You might also want to run

php artisan config:cache

after the change.

Upvotes: 0

Atishay Khare
Atishay Khare

Reputation: 91

This appears to be because the timezone of your server is different to your own.

This could be caused by:

Server misconfiguration
Physical location of the server is in a different timezone
Policies of your provider could also cause this. If your provider decides they want to operate on the same timezone on every server they have throughout the world, this will cause issues.

The server's timezone appears to be CET (Central European Time) which is +1 GMT, as you described.

To fix this, you should change the timezone in your php.ini file (instructions are from the link):

Open your php.ini file
Add the following line of code to top of your php.ini file:

date.timezone = "US/Central"

Alternatively you should replace the US/Central timezone with the desired timezone as outlined here if you wish PHP to use another timezone.

Upvotes: 1

Related Questions