php 时区与 symfony 2

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

Timezone with symfony 2

macossymfonytimezonephp

提问by Saperlipopette

I've got some troubles whith Symfony 2, trying to generate schema via :

我在 Symfony 2 中遇到了一些麻烦,试图通过以下方式生成模式:

php console/app generate:schema:create

I've got a pretty error :

我有一个相当大的错误:

[Exception]                           
DateTime::__construct(): It is not safe to rely on the system's 
timezone settings. You are *required* to use the date.timezone 
setting or the date_default_timezone_set() function.

In case you used any of those methods and you are still 
getting this warning, you most likely misspelled the timezone 
identifier. 

We selected 'Europe/Berlin' for 'CEST/2.0/DST' instead

Thus, I'm trying to change the timezone in my php.ini with

因此,我试图在我的 php.ini 中更改时区

date.timezone = 'Europe/Paris'

but even if I'm reloading my apache, I still get

但即使我重新加载我的 apache,我仍然得到

date.timezone   Europe/Zurich   Europe/Zurich

if I look at my phpinfo(), so I don't really know what should I try next ... And I'm not even sure that I'm in the good way.

如果我查看我的phpinfo(), 所以我真的不知道接下来我应该尝试什么......而且我什至不确定我的方式是否良好。

Thanks for helping me

谢谢你帮助我

回答by Jonathan

On mac, if you have /etc/php.ini.default, do

在 Mac 上,如果有/etc/php.ini.default,请执行

sudo cp /etc/php.ini.default /etc/php.ini 

and then do

然后做

php --ini 

to see that it now loads a php.ini file.

查看它现在加载了一个 php.ini 文件。

回答by danilo

this that trick

这就是把戏

and se /etc/php5/cli/php.iniand /etc/php5/apache2/php.ini

和 se/etc/php5/cli/php.ini/etc/php5/apache2/php.ini

datetime = 'America/Lima'for example

datetime = 'America/Lima'例如

and run command

并运行命令

$ sudo apt-get install php5-sqlite 
$ sudo /etc/init.d/apache2 restart

回答by Johntron

Some third-party distributions of PHP (including MAMP) have hard-coded the timezone. For instance, MAMP 2.0 is hard-coded to Europe/Berlin, and there is no way to change this. Even changing the value in the php.ini template has no effect, because MAMP overwrites your value. Are you using PHP from source, a pre-compiled package, or PHP as a part of some kind of LAMP stack?

PHP 的一些第三方发行版(包括 MAMP)对时区进行了硬编码。例如,MAMP 2.0 被硬编码为欧洲/柏林,并且没有办法改变这一点。即使更改 php.ini 模板中的值也无效,因为 MAMP 会覆盖您的值。您是使用源代码、预编译包还是 PHP 作为某种 LAMP 堆栈的一部分?

回答by gabis

If you are on debian based, you must modify the parameter in the file /etc/php5/cli/php.ini, changing it in /etc/php5/apache2/php.ini has no effect.

如果你是基于debian的,你必须修改/etc/php5/cli/php.ini文件中的参数,在/etc/php5/apache2/php.ini中修改它是无效的。

回答by Darragh Enright

I know this is an old ticket, but since I was having a problem with this too, and it's still open I thought I'd share my quick 'n' dirty solution:

我知道这是一张旧票,但由于我也遇到了这个问题,而且它仍然是开放的,我想我会分享我的快速“n”解决方案:

In my case, date.defaultwas not set at all and I wasn't in a position to change php.ini. So, as a stopgap I added date_default_timezone_set('Europe/Dublin')at the start of web/app.phpto change the timezone at runtime, which worked nicely for me.

就我而言,date.default根本没有设置,我无法更改php.ini. 因此,作为权宜之计,我date_default_timezone_set('Europe/Dublin')在开始web/app.php时添加了在运行时更改时区的方法,这对我来说效果很好。

If you're using the console to run any commands you will probably have to add the call to the the top of this script also.

如果您使用控制台运行任何命令,您可能还必须将调用添加到此脚本的顶部。

回答by Thomas Juma

For Windows, I installed XAMPP for the webserver and downloaded PHP and installed it so that I could run the symfony commands from command prompt.

对于 Windows,我为 Web 服务器安装了 XAMPP 并下载了 PHP 并安装了它,以便我可以从命令提示符运行 symfony 命令。

In this case, the php.inifile used when I execute a Symfony command is located in C:\Program Files(x86)\PHP\php.ini. You have to have admin rights in order to edit this file, so I launched Notepad++ as administrator and opened the file.

在这种情况下,php.ini我执行 Symfony 命令时使用的文件位于C:\Program Files(x86)\PHP\php.ini. 您必须具有管理员权限才能编辑此文件,因此我以管理员身份启动了 Notepad++ 并打开了该文件。

Look for date.timezoneand modify it according to your timezone e.g. date.timezone = Africa/Nairobi. Timezones are listed on the php website, just search php, timezones. No need to restart your computer or webserver.

date.timezone根据您的时区查找并修改它,例如date.timezone = Africa/Nairobi。时区列在 php 网站上,只需搜索php, timezones. 无需重新启动计算机或网络服务器。

回答by lucoweb

I ran into this problem with my shared host. they told me I couldn't change time zones via php.ini, but you can try an .htaccessfile instead.

我的共享主机遇到了这个问题。他们告诉我我无法通过 php.ini 更改时区,但您可以尝试使用.htaccess文件代替。

create the .htaccessin the folder where Symfony is installed and put the following code in it:

.htaccess在安装 Symfony 的文件夹中创建,并将以下代码放入其中:

SetEnv TZ America/Sao_Paulo

hope this helps.

希望这可以帮助。

cheers, Luciano

欢呼,卢西安诺

回答by Bernard

I just encountered this problem with XAMPP on Mac 10.11.5, PHP 5.6.8

我刚刚在 Mac 10.11.5、PHP 5.6.8 上用 XAMPP 遇到了这个问题

My error read "Warning: date_default_timezone_get(): Invalid date.timezone value 'America/Los Angeles',"

我的错误是“警告:date_default_timezone_get():无效的 date.timezone 值‘美国/洛杉矶’,”

I fixed it by finding the only problem was the missing underscore in the "Los Angeles" entry for my PHP.ini date.timezone setting.

我通过发现唯一的问题是我的 PHP.ini date.timezone 设置的“洛杉矶”条目中缺少下划线来修复它。

回答by Vytaliy Makovyak

Try to execute this command:

尝试执行此命令:

sudo ln -s /usr/local/php5/lib/php.ini /etc/php.ini

须藤ln -s /usr/local/php5/lib/php.ini /etc/php.ini

Mac in my case. First - config path, second - don't change.

在我的情况下,Mac。首先 - 配置路径,其次 - 不要改变。