我在 Cake 上遇到了一个奇怪的问题。我住在德国。当我添加帖子时,“已创建”数据库字段中的时间是 6 小时前。例如。下午 6 点创建的帖子在数据库中显示为上午 12 点创建。 我使用托管服务器,并联系了将 php.ini 设置为正确时区欧洲/柏林的公司。当我请求服务器 php.ini 时,它正确显示时区“欧洲/柏林”。但是当我运行代码时
$timezone = date_default_timezone_get();
echo "The current server timezone is: " . $timezone;
在 CakePHP 中显示
The current server timezone is: America/New_York
我已经尝试在 core.php 中设置:
date_default_timezone_set('Europe/Berlin');
但这根本没有效果。 希望有人知道该怎么做。
编辑1:
在要求支持人员运行“locale -a”后,出现以下信息:
# locale -a
C
de_DE
de_DE@euro
de_DE.iso88591
de_DE.iso885915@euro
de_DE.utf8
deutsch
en_US.utf8
german
POSIX
我认为服务器端一切都很好,并且错误是蛋糕式的。 但我不知道下一步该做什么?也许有人可以帮忙。提前致谢。
** 编辑 2:**
在查看了可能的原因后,我在用户管理插件的 bootstrap.php 中发现了错误,该错误将那里的时区设置为 UTC。所以现在一切都很好。不管怎样,谢谢你的帮助。
最佳答案
编辑:
如果执行下面的答案没有效果(根据OP),则可能是this issue :
<小时/>Is is quite likely that the German locale is not installed on the server your running the script on - do you have shell access to the server? Then try
locale -a
to see which locales are installed. Also have a look here Is it feasible to rely on setlocale, and rely on locales being installed?
TLDR:
您的想法是正确的 - 只需将其放入您的 Config/bootstrap.php
中即可。而不是您的 Config/core.php。
更多详细信息:
这可能与 this question 相同。答案是:
Put this in your Config/bootstrap.php:
date_default_timezone_set('UTC'); //or whatever your timezone is
It's just based on the server time and really has nothing to do with CakePHP - so just change the default timezone with PHP, and you should be good to go. 'created' and 'modified' will be based on the specified timezone.
重申一下,“created
”和“modified
”字段是基于服务器的,实际上与 CakePHP 没有太大关系(听起来像你已经假设正确了)。
相关项目(仅供引用)是 CakeTime::convert() ,但在本例中,这不是您想要的。
关于cakephp - Cake PHP时区错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15073309/