0

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

2
  • Thank you for your Answers. The Timezone seems not to be the Problem.I have tried it and it has not fixed my Problem It seems to me, that the server or anything else is saving the last value of the DateTime/Carbon object and is continuing the Time from this point on. I hope i can make myself clear. Commented Dec 14, 2018 at 19:55
  • Check this Laravel return different timestamp from database using eloquent Commented Mar 27, 2023 at 8:33

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.