php 碳现在时间错了

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27732548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:29:44  来源:igfitidea点击:

Carbon now time wrong

phpdatetimelaravelphp-carbon

提问by ambe5960

I just started using the Carbon extension (seems pretty sweet so far), but am confused on the Carbon::now()function. According to the docs, it seems as though this function should reflect the current time in the users current timezone, however, I seem to get a time that is an hour ahead of GMT.

我刚开始使用 Carbon 扩展(到目前为止看起来很不错),但对Carbon::now()功能感到困惑。根据文档,这个函数似乎应该反映用户当前时区的当前时间,但是,我似乎得到了比格林威治标准时间早一个小时的时间。

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

Carbon::now()2015-01-01 17:26:46当我在 PST 上时,它实际上是当前2015-01-01 08:26:46.

Do I have to detect and put in a users local timezone for all instances?

我是否必须为所有实例检测并输入用户本地时区?

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

是什么赋予了?(我很可能对网站如何获得用户本地时间有根本的误解)

采纳答案by AStopher

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.

正如您所描述的,服务器的时区似乎是 CET(欧洲中部时间),即 +1 GMT。

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

要解决此问题,您应该更改php.ini文件中的时区(说明来自链接):

  1. Open your php.inifile
  2. Add the following line of code to top of your php.ini file:
  1. 打开你的php.ini文件
  2. 将以下代码行添加到 php.ini 文件的顶部:

date.timezone = "US/Central"

date.timezone = "美国/中部"

Alternatively you should replace the US/Centraltimezone with the desired timezone as outlined hereif you wish PHP to use another timezone.

或者如果您希望 PHP 使用其他时US/Central区,您应该使用此处概述的所需时区替换时区。

回答by Frank L

Carbon is UTC based therefore simply doing Carbon::now()will output the time in UTC format

Carbon 是基于 UTC 的,因此简单地做Carbon::now()就会以 UTC 格式输出时间

You have to specify the timezone for an accurate reflection of the dateTime in your city or area.

您必须指定时区才能准确反映您所在城市或地区的日期时间。

There are two ways to do this. You can either do:

有两种方法可以做到这一点。你可以这样做:

Carbon::now('PST')OR Carbon::now('Continent/City')like (Carbon::now('America/Montreal')for example

Carbon::now('PST')碳::现在(“洲/城”)一样(Carbon::now('America/Montreal'),例如

回答by Rishi

Change your time zone in config/app.php

在 config/app.php 中更改您的时区

 'timezone' => 'YOUR TIME ZONE',