从客户端提供的时间戳(和时区)获取适当的时间 [英] Get proper time from client-supplied timestamp (and timezone)

查看:127
本文介绍了从客户端提供的时间戳(和时区)获取适当的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个典型的php / html / javascript应用程序。有一个主要的HTML页面有很多javascript和AJAX调用PHP来接收/发送数据。



其他的,应用程序跟踪某些用户操作,记录什么动作是和动作的时间戳。我在javascript中获取时间戳记为 new Date()。getTime()。我将操作日志发送到服务器,该服务器使用 $ dt = date('h:i:s',timestamp / 1000)来获取人类可消化的时间。 (请注意,对于打印目的,我只对时间元素感兴趣,但是我需要将完整的日期/时间存储在数据库中以供以后进行审核。)



我的客户端可以位于世界任何地方。我注意到的是,当从客户端收到的时间戳调用 date 时,服务器应用服务器所在位置的时区偏移量(很可能是默认的时区从 php.ini )。因此,如果在伦敦的客户端传递时间戳为10:00:00,服务器打印的时间最终为2:00:00,因为服务器位于加利福尼亚。



我可以使用数据轻松地发送时区UTC偏移量(即$ code> new Date()的值。getTimezoneOffset())。但是我不太明白我应该如何使用这个时区偏移来获得客户端的正确打印输出。这可能是某种组合,使defualt tz和使用tz从客户端 - 但我只是不能让我的头。



所有我想要的是,10:00:00的时间戳显示为10:00:00,而不管客户端或服务器在哪里。

解决方案

只需设置您正在使用的时区,然后将其更改为用户的时区:

  $ date = new DateTime(null,new DateTimeZone('america / los_angeles')); 
$ date-> setTimeZone(new DateTimeZone('user / timezone')); //用户时区。这一个显然是假的。
echo $ date-> format('H:i:s');

您可以通过以下方式从偏移量获取用户的时区:这个问题/答案

 code> $ offset = -10; 
$ tz = timezone_name_from_abbr(null,$ offset * 3600,true);
if($ tz === false)$ tz = timezone_name_from_abbr(null,$ offset * 3600,false);
echo $ tz;


I have a typical php/html/javascript application. There is one main HTML page with a lot of javascript and AJAX calls to PHP to receive/send data.

Among other things, the app tracks certain user actions, recording what the action is and the timestamp of the action. I'm getting the timestamp in javascript as new Date().getTime(). I send the action log to the server, which uses $dt = date('h:i:s', timestamp / 1000) to get a human-digestable time. (Note that for printing purposes I'm only interested in time element, but I need to store the full date/time in the database for later audits.)

My clients can be located anywhere in the world. What I noticed is that when I call date on the timestamp received from the client, the server applies the timezone offset of where the server is located (most likely, the default timezone from php.ini). Thus, if the client in London is passing a timestamp for 10:00:00, the server-printed time ends up being 2:00:00, as the server is in California.

I can easily send the timezone UTC offset with the data (i.e. the value of new Date().getTimezoneOffset()). However I don't quite understand how I should use this timezone offset to get the right printout for the client. It's probably going to be some sort of combination of getting the defualt tz and using the tz from the client - but I just can't get my head around it.

All I want is that the timestamp for 10:00:00 be displayed as 10:00:00 regardless of where the client is or a server is.

解决方案

Just set the time in the timezone you're working in and then change it to be the user's timezone:

$date = new DateTime(null, new DateTimeZone('america/los_angeles'));
$date->setTimeZone(new DateTimeZone('user/timezone')); // user timezone. this one is clearly fake.
echo $date->format('H:i:s');

You can get the user's timezone from the offset by following this question/answer:

$offset = -10;
$tz = timezone_name_from_abbr(null, $offset * 3600, true);
if($tz === false) $tz = timezone_name_from_abbr(null, $offset * 3600, false);
echo $tz;

这篇关于从客户端提供的时间戳(和时区)获取适当的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆