在 PHP 中使用系统时区设置

发布于 2024-12-22 17:12:10 字数 266 浏览 0 评论 0 原文

所以我有一个用 PHP 编写的 Web 应用程序,它将在世界各地的不同 Ubuntu 服务器上运行。 有些服务器将配置为在本地时间运行,有些将在 UTC 运行,这取决于客户。

虽然我可以编辑 php.ini 文件并设置 date.timezone,但手动输入此类数据肯定有一天会出错。

如何让 PHP 使用系统中已定义的时区(tzdata)?

或者,换句话说:如何从 PHP 系统中提取(长)时区名称以在 date_default_timezone_set() 中使用?

So I have a web app written in PHP that will run on different Ubuntu servers around the world.
Some of the servers will be configured to run on local time, some will run on UTC, it depends on the customer.

While I can edit the php.ini-file and set date.timezone, manually enter such data is bound to get wrong one day.

How can I get PHP to use the time zone already defined in the system (tzdata)?

Or, in other words: How can I extract (the long) time zone name from the system in PHP to use in date_default_timezone_set()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

長街聽風 2024-12-29 17:12:10

在安德烈·克努普(Andrey Knupp)的回答中提供的帮助下,我能够解决这个问题。

echo "Time: " . date("Y-m-d H:i:s") . "<br>\n";

$shortName = exec('date +%Z');
echo "Short timezone:" . $shortName . "<br>";

$longName = timezone_name_from_abbr($shortName);
echo "Long timezone:" . $longName . "<br>";

date_default_timezone_set($longName);
echo "Time: " . date("Y-m-d H:i:s") . "<br>\n";

给出输出:

Time: 2011-12-23 23:29:45
Short timezone:MYT
Long timezone:Asia/Kuala_Lumpur
Time: 2011-12-24 06:29:45

更新:时区的简称不是唯一的。例如,在美国和澳大利亚都有一个“EST”,导致 timezone_name_from_abbr 有时会选择我不想要的那个...您可以询问 date 偏移量有多大,这可以用于进一步匹配正确的时区:

$offset = exec('date +%::z');
$off = explode (":", $offset);
$offsetSeconds = $off[0][0] . abs($off[0])*3600 + $off[1]*60 + $off[2];
$longName = @timezone_name_from_abbr($shortName, $offsetSeconds);

With the help that Andrey Knupp gave in his answer, I was able to solve this one.

echo "Time: " . date("Y-m-d H:i:s") . "<br>\n";

$shortName = exec('date +%Z');
echo "Short timezone:" . $shortName . "<br>";

$longName = timezone_name_from_abbr($shortName);
echo "Long timezone:" . $longName . "<br>";

date_default_timezone_set($longName);
echo "Time: " . date("Y-m-d H:i:s") . "<br>\n";

Gives output:

Time: 2011-12-23 23:29:45
Short timezone:MYT
Long timezone:Asia/Kuala_Lumpur
Time: 2011-12-24 06:29:45

Update: The short name for the time zones are not unique. For instance, in both America and Australia there is an "EST", leading timezone_name_from_abbr to sometimes pick the one I don't want to... You can ask date how big the offset is, and that can be used to further match the correct time zone:

$offset = exec('date +%::z');
$off = explode (":", $offset);
$offsetSeconds = $off[0][0] . abs($off[0])*3600 + $off[1]*60 + $off[2];
$longName = @timezone_name_from_abbr($shortName, $offsetSeconds);
哆兒滾 2024-12-29 17:12:10

我最近使用过几次,但我不知道它适用于哪些 Linux 发行版:

date_default_timezone_set(trim(file_get_contents('/etc/timezone')));

也许 timedatectl 更可预测:

$ timedatectl show --property=Timezone
Timezone=Europe/Amsterdam

所以这也很简单:

date_default_timezone_set(trim(substr(`timedatectl show --property=Timezone`, 9)));

I've used this a few times recently, but I don't know on which Linux distros this works:

date_default_timezone_set(trim(file_get_contents('/etc/timezone')));

Maybe timedatectl is more predictable:

$ timedatectl show --property=Timezone
Timezone=Europe/Amsterdam

so that's pretty easy too:

date_default_timezone_set(trim(substr(`timedatectl show --property=Timezone`, 9)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文