简体   繁体   English

函数 date_default_timezone_set() 不可用

[英]Function date_default_timezone_set() is not available

As the title says, can anybody explain why the code正如标题所说,任何人都可以解释为什么代码

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

works but有效但

date_default_timezone_set( 'Europe/Athens' );

does not?才不是? It gives me this error:它给了我这个错误:

Fatal error: Call to undefined function date_default_timezone_set()致命错误:调用未定义的函数 date_default_timezone_set()

I was looking for a solution to convert a timestamp and all I could find on the web was to use date_default_timezone_set .我正在寻找一种转换时间戳的解决方案,我在网上能找到的只是使用date_default_timezone_set But the conversion had always 1 hour difference.但转换总是有 1 小时的差异。

Then I found this topic " strftime() function showing incorrect time " which is using the init_set code (and afterwards I've RTM and found out that it's already mentioned in the documentation).然后我发现了这个主题“ strftime()函数显示不正确的时间”,它使用了 init_set 代码(后来我使用了 RTM 并发现它已经在文档中提到了)。 I tried it and it worked.我试过了,它奏效了。

But I haven't found an answer why date_default_timezone_set is not working.但我还没有找到为什么date_default_timezone_set的答案。

Can anybody explain please?有人可以解释一下吗?

You must be using an old version of PHP.您必须使用旧版本的 PHP。

date_default_timezone_set is for PHP 5.1.2 and above.

Try php -v and check your PHP version.尝试php -v并检查您的 PHP 版本。

您可以使用:

putenv("TZ=Europe/Berlin"); // PHP 4

localhost本地主机

server服务器

It's quite late, I got the same issue and I see there is the difference, I post the screenshot from my localhost and my personal server phpinfo for the timezone.已经很晚了,我遇到了同样的问题,我看到了不同之处,我发布了来自我的本地主机和我的个人服务器 phpinfo 的时区屏幕截图。

On my localhost, date_default_timezone_set('[timezone]') works correctly, however, on the server, it always return the UTC.在我的本地主机上, date_default_timezone_set('[timezone]') 工作正常,但是,在服务器上,它总是返回 UTC。 Based on the php doc, it looks like the timezone is not set in the php ini of my server, so i likely ignored the the timezone set since no value is defined previously.根据 php 文档,我的服务器的 php ini 中似乎没有设置时区,所以我可能忽略了时区设置,因为之前没有定义任何值。

Using the init_set, it makes the timezone to be available.使用 init_set,它使时区可用。 I think the timezone is not predefined on the php ini is the cause.我认为时区未在 php ini 上预定义是原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM