php:通过 UTC 偏移量设置时区

发布于 2024-12-02 16:39:43 字数 250 浏览 0 评论 0原文

使用 javascript 我知道我的用户时区是 UTC +3。

现在我想用这些知识创建 DateTime 对象:

$usersNow = new DateTime('now', new DateTimeZone("+3"));

我收到响应:

'Unknown or bad timezone (+2)'

我做错了什么?我该如何修复?

Using javascript I know that my users timezone is UTC +3.

Now I want to create DateTime object with this knowledge:

$usersNow = new DateTime('now', new DateTimeZone("+3"));

I receive as a respsonse:

'Unknown or bad timezone (+2)'

What am I doing wrong? How can I fix?

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

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

发布评论

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

评论(10

悲欢浪云 2024-12-09 16:39:43

这个怎么样...

$original = new DateTime("now", new DateTimeZone('UTC'));
$timezoneName = timezone_name_from_abbr("", 3*3600, false);
$modified = $original->setTimezone(new DateTimezone($timezoneName));

how about this...

$original = new DateTime("now", new DateTimeZone('UTC'));
$timezoneName = timezone_name_from_abbr("", 3*3600, false);
$modified = $original->setTimezone(new DateTimezone($timezoneName));
一腔孤↑勇 2024-12-09 16:39:43

你说:

使用 javascript 我知道我的用户时区是 UTC +3。

您可能运行了类似这样的命令:

var offset = new Date().getTimezoneOffset();

这将返回与 UTC 的当前偏移量(以分钟为单位),正值位于 UTC 以西。它返回时区!

时区不是偏移量。时区偏移量。它可以有多个不同的偏移量。通常有两种偏移,一种用于标准时间,另一种用于夏令时。单个数值无法单独代表这一点。

  • 时区示例:“America/New_York”
    • 对应的标准偏移量:UTC-5
    • 对应的日光偏移:UTC-4

除了这两个偏移量之外,该时区中还包含以下日期和时间在两个偏移量之间进行转换,以便您知道它们何时应用。还有关于偏移和过渡如何随时间变化的历史记录。

另请参阅时区标签 wiki 中的“时区 != 偏移量”。

在您的示例中,您可能从 javascript 收到 -180 值,表示 UTC+3 的当前偏移量。但这只是该特定时间点的偏移量!如果您遵循 minaz 的回答,您将获得一个时区,该时区假设 UTC+3始终< /em> 正确的偏移量。如果实时时区类似于 "Africa/Nairobi"从未使用过除 UTC+3 之外的任何内容。但据您所知,您的用户可能位于 “Europe/Istanbul”,夏季使用 UTC+3,冬季使用 UTC+2。

You said:

Using javascript I know that my users timezone is UTC +3.

You probably ran something like this:

var offset = new Date().getTimezoneOffset();

This returns the current offset from UTC in minutes, with positive values falling west of UTC. It does not return a time zone!

A time zone is not an offset. A time zone has an offset. It can have multiple different offsets. Often there are two offsets, one for standard time and one for daylight saving time. A single numeric value cannot represent this alone.

  • Example of a time zone: "America/New_York"
    • Corresponding standard offset: UTC-5
    • Corresponding daylight offset: UTC-4

Besides the two offsets, also wrapped up in that time zone are the dates and times for transitioning between the two offsets so you know when they apply. There's also a historical record of how the offsets and transitions may have changed over time.

See also "Time Zone != Offset" in the timezone tag wiki.

In your example case, you probably received a value of -180 from javascript, representing a current offset of UTC+3. But that's just the offset for that particular point in time! If you follow minaz's answer, you will get a time zone that makes the assumption that UTC+3 is always the correct offset. That would work if the real time zone is something like "Africa/Nairobi" which has never used anything except UTC+3. But for all you know your user could be in "Europe/Istanbul", which uses UTC+3 in the summer and UTC+2 in the winter.

愛上了 2024-12-09 16:39:43

现代答案:

$usersNow = new DateTime('now', new DateTimeZone('+0300'));

文档:

http://php.net/manual/en/datetimezone.construct.php< /a>


Modern answer:

$usersNow = new DateTime('now', new DateTimeZone('+0300'));

Documentation:

http://php.net/manual/en/datetimezone.construct.php

看海 2024-12-09 16:39:43

从 PHP 5.5.10 开始,DateTimeZone 接受像“+3”这样的偏移量:

https://3v4l.org/NUGSv

Since PHP 5.5.10, DateTimeZone accepts an offset like "+3" :

https://3v4l.org/NUGSv

凉月流沐 2024-12-09 16:39:43

这使 Matthew 的答案更进一步,将日期的时区更改为任何整数偏移量。

public static function applyHourOffset(DateTime $dateTime, int $hourOffset):DateTime
{
    $dateWithTimezone = clone $dateTime;

    $sign = $hourOffset < 0 ? '-' : '+';
    $timezone = new DateTimeZone($sign . abs($hourOffset));
    $dateWithTimezone->setTimezone($timezone);

    return $dateWithTimezone;
}

注意:由于接受的答案,我在生产中遇到了中断。

This one takes Matthew's answer a step further to change the timezone of a date to any integer offset.

public static function applyHourOffset(DateTime $dateTime, int $hourOffset):DateTime
{
    $dateWithTimezone = clone $dateTime;

    $sign = $hourOffset < 0 ? '-' : '+';
    $timezone = new DateTimeZone($sign . abs($hourOffset));
    $dateWithTimezone->setTimezone($timezone);

    return $dateWithTimezone;
}

Note: I had a breakage in production due to the accepted answer.

鹿港巷口少年归 2024-12-09 16:39:43

您是否尝试使用 strtotime()

 <?php
    echo strtotime("now"), "\n";
    echo strtotime("10 September 2000"), "\n";
    echo strtotime("+5 hours");
    echo strtotime("+1 day"), "\n";
    echo strtotime("+1 week"), "\n";
    echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
    echo strtotime("next Thursday"), "\n";
    echo strtotime("last Monday"), "\n";

Did you try using strtotime()?

 <?php
    echo strtotime("now"), "\n";
    echo strtotime("10 September 2000"), "\n";
    echo strtotime("+5 hours");
    echo strtotime("+1 day"), "\n";
    echo strtotime("+1 week"), "\n";
    echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
    echo strtotime("next Thursday"), "\n";
    echo strtotime("last Monday"), "\n";
暗喜 2024-12-09 16:39:43

据我从 DateTimeZone 上的文档得知,您需要传递有效的时区,这里是有效的时区。检查其他,那里的东西可能会帮助你。

As far as I can tell from the docs on DateTimeZone, you need to pass a valid time zone and here are the valid ones. Check the others, something there may help you.

南城旧梦 2024-12-09 16:39:43

DateTimeZone 需要时区而不是offest

DateTimeZone requires a timezone not an offest

一口甜 2024-12-09 16:39:43

对于遇到此问题的任何人,我都面临着同样的问题,因此最终我扩展了 DateTime 类并覆盖了 __construct() 方法以接受偏移量(以分钟为单位)而不是时区。

从那里,我的自定义 __construct() 计算出以小时和分钟为单位的偏移量(例如 -660 = +11:00),然后使用 parent::__construct() 将我的日期(自定义格式以包含我的偏移量)交回原始日期时间。

因为我总是在应用程序中处理 UTC 时间,所以我的类还通过减去偏移量来修改 UTC 时间,因此传递午夜 UTC 和 -660 的偏移量将显示上午 11 点。

我的解决方案详细信息如下: https://stackoverflow.com/a/35916440/2301484

For anyone who comes across this, I was facing the same problem, so in the end I extended the DateTime class and overrode the __construct() method to accept an offset (in minutes) instead of a timezone.

From there, my custom __construct() works out what the offset is in hours and minutes (e.g. -660 = +11:00) and then uses parent::__construct() to hand my date, custom formatted to include my offset, back to the original DateTime.

Because I'm always dealing with UTC times in my application, my class also modifies the UTC time by subtracting the offset, so passing Midnight UTC and an offset of -660 will show me 11am

My solution is detailed here: https://stackoverflow.com/a/35916440/2301484

忆离笙 2024-12-09 16:39:43

感谢 Joey Rivera 的链接,我找到了一个解决方案。就像其他人在这里所说的那样,时区不是偏移量,您确实需要一个有效的时区。

这就是我自己使用的,

$singapore_time = new DateTime("now", new DateTimeZone('Asia/Singapore'));


var_dump( $singapore_time );

我自己发现使用 YYYY-MM-DD HH:MM 格式要方便得多。例子。

$original = new DateTime("2017-05-29 13:14", new DateTimeZone('Asia/Singapore'));

I was directed to a solution thanks to Joey Rivera's link. Like what the others have said here, timezone is not an offset you do need a valid timezone.

This is what I am using for myself

$singapore_time = new DateTime("now", new DateTimeZone('Asia/Singapore'));


var_dump( $singapore_time );

I myself have found it to be much more convenient to work with YYYY-MM-DD HH:MM format. example.

$original = new DateTime("2017-05-29 13:14", new DateTimeZone('Asia/Singapore'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文