Carbon is wrong now

I just started using the Carbon extension (itโ€™s still pretty sweet), but I got confused in the Carbon::now() function. According to the docs, it seems that this function should reflect the current time in the current time zone of users, however, it seems that I get a time that is an hour ahead of GMT.

i.e. Carbon::now() says 2015-01-01 17:26:46 when I am in PST, and actually it is currently 2015-01-01 08:26:46 .

Should I detect and set the local time zone of users for all instances?

What gives? (I may very well have a fundamental misunderstanding of how the site gets local time for users)

+6
source share
2 answers

It looks like the time zone of your server is different from your own.

This may be caused by:

  • Server configuration
  • The physical location of the server is in a different time zone.
  • Your ISP policies may also cause this. If your vendor decides that they want to work in the same time zone on every server they have around the world, this will cause problems.

The serverโ€™s time zone, in your opinion, is CET (Central European Time), which, as you described, is +1 GMT.

To fix this, you must change the time zone in the php.ini (instructions are from the link):

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

date.timezone = "US / Central"

Alternatively, you should replace the US/Central time zone with the required time zone as described here if you want PHP to use a different time zone.

+7
source

Carbon is based on UTC, so just Carbon::now() displays the time in UTC

You must specify a time zone to accurately reflect the date in your city or region.

There are two ways to do this. You can:

Carbon::now('PST') OR Carbon :: now ('Continent / City'), e.g. (Carbon::now('America/Montreal')

+8
source

Source: https://habr.com/ru/post/980399/


All Articles