如何在网页中将 UTC 时间显示为当地时间?

发布于 2024-08-31 09:37:19 字数 134 浏览 12 评论 0原文

我有一个保存 UTC 时间的数据库。这个时间可以显示在网页中,因此我被要求在页面中将其显示为当地时间,因为可以从任何国家/地区查看它。一位同事提到了有关从当前线程(在服务器上)获取国家/地区设置的信息,但我找不到任何有关此的详细信息。我想做的事情可能吗?

I have a database that holds a time as UTC. This time can be shown in a webpage, so I’ve been asked to show it as local time in the page as it can be viewed from any country. A colleague mentioned something about getting the country settings from the current thread (on the server) but I couldn’t find any details on this. Is what I want to do possible?

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

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

发布评论

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

评论(5

幸福丶如此 2024-09-07 09:37:20

这就是我在连接到 MySQL 数据库的登录文件中在 PHP 中完成的方法:

//set local timezone
date_default_timezone_set('America/Denver'); //use local TZ

然后,每次访问数据库时,您都会为该会话以及与其关联的任何输入/输出设置时区。

this is how I've done it in PHP in the login file which connects to MySQL database:

//set local timezone
date_default_timezone_set('America/Denver'); //use local TZ

Then, every time you access the DB, you are setting the timezone for that session and any input/output associated with it.

世态炎凉 2024-09-07 09:37:19

如果您(和您的网站)熟悉 JavaScript,那么有一种非常简单的方法可以实现此目的。

首先,在服务器端,您将采用 RFC 3339 格式(calendar 等协议使用的互联网时间标准)格式化 UTC 日期/时间。 RFC 3339 的基本语法是:

YYYY-MM-DDTHH:MM:SS

这样,我在哪里,时间将是:

2010-05-04T05:52:33

但是当时间不是本地时间而是 UTC 时,您可以在末尾添加一个 Z 来表示这一点。因此,就我而言,由于我距离 GMT 为 -0500 小时,因此上面的同一时间将是:

2010-05-04T10:52:33Z

因此,首先让服务器将以上内容输出到网页的 javascript。然后 JavaScript 可以解析该时间戳,并输出调整为浏览器时区(由托管浏览器的计算机确定)的日期和时间。您应该记住,如果用户来自东京并在西班牙查看您的网站,他们将看到东京时间的时间戳,除非他们调整了计算机时钟。

所以 javascript 将是:

    var time_string_utc = some_server_variable; // timestamp from server
    var time_string_utc_epoch = Date.parse(time_string_utc);
    var time_utc = new Date();
    time_utc.setTime(time_string_utc_epoch);

此时,您已经将 javascript 日期对象设置为 UTC 时间戳。对上面发生的情况的快速解释:

第一个变量假设您已将时间戳字符串从服务器传递给该变量。

第二个变量使用 Date.parse() 方法将字符串转换为纪元时间戳。

第三个变量创建未设置的 Date 对象。

最后一行使用 setTime 方法,该方法根据纪元时间戳设置 Date 对象。

现在您已经有了该对象,您可以根据需要将其输出给用户。作为一个简单的实验,你可以使用:

document.write(time_utc);

如果你在我的时区,使用 UTC 时间戳,我开始使用:

2010-05-04T10:52:33Z

会给出:

 Tue May 04 2010 05:52:33 GMT-0500 (CST)

但你可以使用各种 JavaScript 方法将时间格式化为更令人愉悦的内容。

无需猜测用户的国家/地区,甚至无需调整时间戳,只要您信任用户的本地浏览器/计算机时区即可。

再次,简短版本:

    var time_string_utc = some_server_variable; // UTC time from server
    var time_string_utc_epoch = Date.parse(some_server_variable);
    var time_utc = new Date();
    time_utc.setTime(time_string_utc_epoch);
    document.write(time_utc);

If you (and your website) are comfortable with javascript, there is a very easy way to accomplish this.

First, on the server side, you would have the UTC date/time formatted in RFC 3339 format (the standard for internet time used by, among other protocols, icalendar). The basic syntax of RFC 3339 is:

YYYY-MM-DDTHH:MM:SS

Such that where I am, the time would be:

2010-05-04T05:52:33

But when the time is not local, but UTC, you add a Z to the end to denote this. So in my case, since I'm at -0500 hours from GMT, the same time above would be:

2010-05-04T10:52:33Z

So, first you get the server to output the above to your web page's javascript. The javascript can then parse that timestamp and it will output the date and time adjusted to the browser's time zone (which is determined by the computer hosting the browser). You should remember that if a user is from Tokyo and is viewing your website in Spain, they will see the timestamp in Tokyo time unless they've adjusted their computer's clock.

So the javascript would be:

    var time_string_utc = some_server_variable; // timestamp from server
    var time_string_utc_epoch = Date.parse(time_string_utc);
    var time_utc = new Date();
    time_utc.setTime(time_string_utc_epoch);

At this point, you have a javascript date object set to your UTC timestamp. A quick explanation of what happens above:

The first variable assumes you have passed the timestamp string to that variable from the server.

The second variable uses the Date.parse() method to convert the string to an epoch timestamp.

The third variable creates the unset Date object.

The last line line uses setTime method, which sets a Date object from an epoch timestamp.

Now that you have the object, you can output it to the user as you see fit. As a simple experiment, you can use:

document.write(time_utc);

which, if you are in my timezone using the UTC timestamp I started off with:

2010-05-04T10:52:33Z

would give:

 Tue May 04 2010 05:52:33 GMT-0500 (CST)

but you can use various javascript methods to format the time into something much more pleasant looking.

No need to guess the user's country or even adjust your timestamp, so long as you trust the user's local browser/computer time zone.

Again, the short version:

    var time_string_utc = some_server_variable; // UTC time from server
    var time_string_utc_epoch = Date.parse(some_server_variable);
    var time_utc = new Date();
    time_utc.setTime(time_string_utc_epoch);
    document.write(time_utc);
×纯※雪 2024-09-07 09:37:19

另请考虑阅读此帖子,其中深入探讨了处理日光的注意事项跨时区保存问题

Also consider reading this thread which dives deeper in the dos and donts of handling daylight saving problem across timezones

完美的未来在梦里 2024-09-07 09:37:19

当然是可能的。您只需要找到国家/地区设置来检测您的用户来自哪个国家/地区,然后修改显示的日期以适应该国家/地区的时间。您还可以找到另一种方法来检测用户所在的国家/地区。(也许从他的 IP 地址)。

我确信管理此问题的最佳方法是让您的同事知道您需要有关项目中国家/地区设置实施以及如何使用它的更多详细信息。

Of-course is possible. You just need to find that country settings to detect the country your user comes from, and then modify the displayed date to fit that country's time.You can also find another way to detect the user country.(maybe from his ip address).

I am sure that the best way to manage this is let your colleague know that you need more details about the country settings implementation on your project and how can you use it.

纵情客 2024-09-07 09:37:19

编辑:

考虑一下,我认为一旦掌握了客户的文化就不可能显示当地时间。我当然有兴趣看看你同事的建议。

例如,了解美国文化并没有帮助,因为美国有很多时区。

我认为像 Anthony 建议的那样使用 Javascript 是可行的方法...

旧:

您可以覆盖 InitializeCulture() 方法,其中包含设置当前(选择的或浏览器报告)区域性的代码:

Thread.CurrentThread.CurrentCulture = 
            CultureInfo.CreateSpecificCulture(selectedLanguage);
Thread.CurrentThread.CurrentUICulture = new 
            CultureInfo(selectedLanguage);

EDIT:

Thinking about it I don't think it is possible to display the local time once you have the client’s culture. I'd certainly be interested to see your colleagues suggestion.

Getting the US culture, for example, won't help as the US has many timezones.

I think using Javascript like Anthony suggests would be the way to go...

OLD:

You can override the InitializeCulture() method with code that sets the current (chosen or browser reporting) cultures:

Thread.CurrentThread.CurrentCulture = 
            CultureInfo.CreateSpecificCulture(selectedLanguage);
Thread.CurrentThread.CurrentUICulture = new 
            CultureInfo(selectedLanguage);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文