如何应用通过 JS 提供的时区偏移量?

发布于 2024-12-28 15:53:39 字数 501 浏览 3 评论 0原文

假设我用以下方法检测用户时区偏移量:

var tz = (new Date).getTimezoneOffset()/60;

并将 -5 的值发送到 PHP,因此:

<?php

date_default_timezone_set('UTC');

$tz = $_POST['tz']; // -5
$date = strtotime('now');

我想将时间戳作为 UTC 存储在数据库中,但是当显示给用户时我想应用时区偏移。因此,假设 EST 的 $tz-5,我如何将其应用于时间戳以显示 date('M d YH:i') 在我当地的时区?

额外问题:如何才能让它只显示太平洋标准时间的时间?我假设通过找到 PST 偏移量并为原始答案中使用的方程指定硬编码的 PST 编号。

Let's say I detect the users timezone offset with:

var tz = (new Date).getTimezoneOffset()/60;

and send the value of -5 to PHP, so:

<?php

date_default_timezone_set('UTC');

$tz = $_POST['tz']; // -5
$date = strtotime('now');

I want to store the timestamp as UTC in the database, but when displayed to the user I want to apply the timezone offset. So assuming $tz is -5 for EST, how can I apply it to the timestamp in order to display a date('M d Y H:i') in my local timezone ?

Bonus question: How can I have it show time for PST only? I assume by finding the PST offset and just specifying a hardcoded PST number to the equation that will be used in the original answer.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文