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

2022-01-16 00:00:00 datetime timezone php

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

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

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

Now I want to create DateTime object with this knowledge:

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

我收到了回复:

'Unknown or bad timezone (+2)'

我做错了什么?我该如何解决?

What am I doing wrong? How can I fix?

推荐答案

这个怎么样...

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

相关文章