如何确定 UTC 与服务器时区的偏移量?

发布于 2024-08-14 11:34:06 字数 1064 浏览 4 评论 0原文

我发现了很多关于 UTC 表和 php 日期方法来转换它的示例,但我仍然错过了在获取服务器日期后将其转换为我的网页上的用户时区选择的简单方法。

在此页面http://vkham.com/UTC.html 我找到了一个清晰的指南来了解范围,但是我不知道如何连接,例如桌子上的“欧洲/罗马”,所以有更清楚的内容吗?

我知道我的服务器的时区(美国/芝加哥),但我仍然不知道一种将其从 UTC 方法更改为从用户计算机中选择的不同时区(例如“欧洲/罗马”)的方法

我尝试了一些东西,但我仍然错过了一些东西,而且我不知道它是什么:

<?php
$timezone = date ("e");
$date = date ('Y-m-d H:i:s');
print $date." - this is my server date, and his timezone is - $timezone<br/>";

$user_timezone = "Europe/Rome"; // there is a way to convert Europe/Rome to UTC -13?
$selected_timezone = "-13"; // is -13 like Europe/Rome in all cases or only because my server is America/Chicago?
$date_user = date ("Y-m-d H:i:s $selected_timezone");
$str_date_user =  strtotime ($date_user);
$new_user_date = date ('Y-m-d H:i:s', $str_date_user);
print $new_user_date . " - this is my server date, and his timezone is - $user_timezone";
?>

不是否有办法将 UTC 时区的欧洲/罗马转换为 -13?
-13 在所有情况下都像欧洲/罗马,还是仅仅因为我的服务器是美国/芝加哥?

I've found many examples about UTC tables and php date methods to convert it, but I still miss a simple way after got server date, to converting it into an user timezone selection on my web page.

On this page http://vkham.com/UTC.html I've found a clear guide to understand the range, but I don't know how to connect for example "Europe/Rome" on the table, so there is something more clear about it?

I know the timezone of my server (America/Chicago) but I still don't know a way to change it from UTC method to a different timezone selected from the user machine (for example "Europe/Rome")

I tryied something, but I'still miss something, and i don't know what is it:

<?php
$timezone = date ("e");
$date = date ('Y-m-d H:i:s');
print $date." - this is my server date, and his timezone is - $timezone<br/>";

$user_timezone = "Europe/Rome"; // there is a way to convert Europe/Rome to UTC -13?
$selected_timezone = "-13"; // is -13 like Europe/Rome in all cases or only because my server is America/Chicago?
$date_user = date ("Y-m-d H:i:s $selected_timezone");
$str_date_user =  strtotime ($date_user);
$new_user_date = date ('Y-m-d H:i:s', $str_date_user);
print $new_user_date . " - this is my server date, and his timezone is - $user_timezone";
?>

Doesn't exist a way to convert Europe/Rome to -13 for UTC timezone?
Is -13 like Europe/Rome in all cases or only because my server is America/Chicago?

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

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

发布评论

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

评论(1

心在旅行 2024-08-21 11:34:06

您可以使用 gmdate 生成一个日期显示 UTC 时间 - 无论您的服务器运行在哪个时区。然后您只需将时区差异添加为 hours * 3600 到您用于生成日期的时间戳即可获取用户的时间。

另一种方法是使用 date_default_timezone_set

第一个想法的一个简单示例如下:

<?php
$offset   = -13 * 3600; // timezone offset for UTC-13
$utcTime  = gmdate( 'd.m.Y H:i' );
$userTime = gmdate( 'd.m.Y H:i', time() + $offset );
?>

You can use gmdate to generate a date that is displaying the UTC time - whatever timezone your server is running in. Then you can simply add the timezone difference as hours * 3600 to the timestamp you use for generating the date to get the user's time.

A different way would be setting the local time temporary to the user's timezone by using date_default_timezone_set.

A simple example for the first idea would be the following:

<?php
$offset   = -13 * 3600; // timezone offset for UTC-13
$utcTime  = gmdate( 'd.m.Y H:i' );
$userTime = gmdate( 'd.m.Y H:i', time() + $offset );
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文