php DateTimeZone 错误:未知或错误的时区

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

DateTimeZone error: Unknown or bad timezone

php

提问by Falya

I am trying to run this script:

我正在尝试运行此脚本:

<?php
$d = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
$time = $d->format('H:i');

echo $time;
?>

but I got this error:

但我收到了这个错误:

Fatal error: Uncaught exception 'Exception' with message 'DateTimeZone::__construct() [<a href='function.DateTimeZone---construct'>function.DateTimeZone---construct</a>]: Unknown or bad timezone (Asia/Kolkata)

Fatal error: Uncaught exception 'Exception' with message 'DateTimeZone::__construct() [<a href='function.DateTimeZone---construct'>function.DateTimeZone---construct</a>]: Unknown or bad timezone (Asia/Kolkata)

Though it works well for Asia/Daccafor example. What could be the problem and how to fix it?

虽然它工作良好Asia/Dacca,例如。可能是什么问题以及如何解决它?

回答by Alin Purcaru

And welcome to StackOverflow! If you haven't already make some time to read the FAQ.

欢迎来到 StackOverflow!如果您还没有时间阅读常见问题解答。

I tried your example and it worked for me with PHP 5.3.1 and "Olson" Timezone Database Version 2009.18.

我试过你的例子,它对我有用PHP 5.3.1 和 "Olson" Timezone Database Version 2009.18

You should do a phpinfo()and see what time zone DB version you have and if it's older update it. You can see in this listthat the most recent version, 2011.1, has Asia/Kolkata.

你应该做一个phpinfo()看看你有什么时区数据库版本,如果它是旧的更新它。您可以在此列表中看到最新版本 2011.1 包含 Asia/Kolkata。

回答by Elzo Valugi

Try this for fun.

试试这个很有趣。

$timezones = array('Europe/London', 'Mars/Phobos', 'Asia/Kolkata');

foreach ($timezones as $tz) {
    try {
        $mars = new DateTimeZone($tz);
    } catch(Exception $e) {
        echo $e->getMessage() . '<br />';
    }
}

回答by oabarca

PHP timezone database has been updated in version 5.3. Try installing that and try again

PHP 时区数据库已在 5.3 版中更新。尝试安装并重试

回答by Alex

I've got same problem when try to run webserver in CHROOT jail. In this case chrooted dir can not contain Linux timezone files. If it's your case and you have access to webserver files simple copy all files to chroot jail:

尝试在 CHROOT jail 中运行网络服务器时,我遇到了同样的问题。在这种情况下,chrooted 目录不能包含 Linux 时区文件。如果这是您的情况并且您可以访问网络服务器文件,只需将所有文件复制到 chroot jail:

For CentOS it's

对于 CentOS,它是

cp -R /usr/share/zoneinfo/ /MY_CHROOT_DIR/usr/share/