如何应用通过 JS 提供的时区偏移量?
假设我用以下方法检测用户时区偏移量:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论