php wamp服务器中错误的PHP日期()输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5753699/
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 22:23:09  来源:igfitidea点击:

Wrong PHP date() output in wamp server

phpwampserver

提问by anon

The problem is that date('r') returns wrong time for my timezone.

问题是 date('r') 为我的时区返回错误的时间。

php.ini timezone setting:
date.timezone = Europe/Kiev

php.ini 时区设置:
date.timezone = Europe/Kiev

date_default_timezone_set('Europe/Kiev') in my script solves the problem.

我的脚本中的 date_default_timezone_set('Europe/Kiev') 解决了这个问题。

So what's wrong with WAMP?

那么 WAMP 有什么问题呢?

回答by Wh1T3h4Ck5

I suggest always using date_default_timezone_set()from script

我建议始终使用脚本中的date_default_timezone_set()

e.g.

例如

date_default_timezone_set('Europe/Kiev');

or

或者

ini_set('date.timezone', 'Europe/Kiev');

...to avoid PHP guessing timezone.

...避免 PHP 猜测时区。

It comes handy when you transfer code to different server(s), for example, outside of Ukraine. This line should help you to avoid unexpected (wrong) results if date.timezoneis not set in php.inior its setting is incorrect. It's also handy when you can't access and/or modify php.ini(shared hosting).

当您将代码传输到不同的服务器(例如,乌克兰境外)时,它会派上用场。如果date.timezone未设置php.ini或设置不正确,此行应帮助您避免意外(错误)结果。当您无法访问和/或修改php.ini(共享主机)时,它也很方便。

Also, be sure that you've not used ;at the and of line in php.ini.

另外,请确保您没有;php.ini.

Restart server after changing php.ini.

更改后重启服务器php.ini

回答by Cendak

Edit php.iniand restart Apache:

编辑php.ini并重启 Apache:

  • left click to WampServer in tray icon
  • open php.ini(go to PHP -> php.ini)
  • set new date.timezonevalue
  • 左键单击托盘图标中的 WampServer
  • 打开php.ini(转到PHP -> php.ini
  • 设置新date.timezone
    ;date.timezone = UTC 
    date.timezone = Europe/Kiev
  • restart Apache ( go to Apache -> Service -> Restart Service)
  • check value of date.timezoneby phpinfo();
  • 重新启动 Apache(转到Apache -> 服务 -> 重新启动服务
  • 检查date.timezoneby 的值phpinfo();

回答by mauris

You need to reload the configuration / restart the server after editing your php.ini file.

编辑 php.ini 文件后,您需要重新加载配置/重新启动服务器。

回答by Oswald

According to the documentation of date_default_timezone_get, the date.timezoneconfiguration option can be overridden by setting the TZenvironement variable (which, in turn, can be overridden by calling date_default_timezone_set). From your description, I suspect that the TZenvironement veriable is set.

根据 的文档date_default_timezone_getdate.timezone可以通过设置环境TZ变量来覆盖配置选项(反过来,可以通过调用来覆盖date_default_timezone_set)。根据您的描述,我怀疑环境TZ可验证已设置。

回答by Darien Daud

I know this is an old question. If you are using a PHP framework, you might want to check the config file of the framework. For example in Laravel, open the config/app.phpand you will find timezone there. Set it to your timezone.

我知道这是一个老问题。如果您使用的是 PHP 框架,您可能需要检查该框架的配置文件。例如在 Laravel 中,打开config/app.php,你会在那里找到时区。将其设置为您的时区。

回答by Hassan Saeed

by default it shows GMT time you can change for your region with following code

默认情况下,它显示 GMT 时间,您可以使用以下代码更改您所在地区的时间

   date_default_timezone_set("Asia/Bangkok");//set you countary name from below timezone list
    echo $date = date("Y-m-d H:i:s", time());//now it will show "Asia/Bangkok" or your date time

List of Supported Timezones http://www.php.net/manual/en/timezones.php

支持的时区列表 http://www.php.net/manual/en/timezones.php

回答by Mwangi Thiga

  1. Open your php.ini with maybe notepad++, sublime text...
  2. Add this line to the file: date.timezone = "X" Where X is the your time-zone of wish. Get a list of supported timezones here: http://php.net/manual/en/timezones.php
  1. 用记事本++打开你的php.ini,崇高的文字......
  2. 将此行添加到文件中: date.timezone = "X" 其中 X 是您希望的时区。在此处获取支持的时区列表:http: //php.net/manual/en/timezones.php

... That should do it. 3. Restart your server

......应该这样做。3. 重启你的服务器