使用 Ratchet WebSocket,我正在尝试显示已发送消息的时间戳。我console.log(e)
在 onmessage 回调内部,我得到一个对象:
{
//... rest of the keys
timeStamp: 353443
}
考虑到我在 13:48 发送它,我觉得它看起来很奇怪。奇怪的是,之前,我在最后一个小时内发送了一个请求,它返回了一个 5 位数的时间戳,这让我的研究变得更加困难。
我尝试在 Ratchet 文档中查找该领域,但找不到任何内容。我立即尝试通过在线转换来查看它是否是 UNIX 时间戳,但所有值都返回
1 Jan 1970 some:time
如何将返回的时间戳转换为
d/m/y H:i
?我找不到任何关于它可能采用的格式或如何转换的信息。目前我有(在我的 jQuery 中):
new Date(data.timeStamp*1000)
但它返回 1 Jan 的事情。
我试过两次:
Thursday 6th September 2018 14:03:(seconds was roughly 13)
Thursday 6th September 2018 14:19:24
这产生了这些时间戳(分别):
0 1 2 3 3 8 4
2 1 9 5 6 8 3
(显示没有空格)
(注意:前导 0 不在输出中 - 仅用于列比较)
我尝试比较两者,除了 Minute 和 second 之外的所有内容都应该相同,但是 - 只有两列匹配。
我怎么可能认为它可能是(但事实证明它不能基于结果的不同):
Thursday has the potential to be 3 or 4 depending if the coder has set Sunday or Monday to 0
6th should logically only equate to 6
Sept could be 8 if jan = 0
2018 could be 18
14 could be 2 if 12 hour with a flag setting am/pm
这是我的 Chat.php 类:
<?php
namespace App;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface
{
protected $clients;
public function __construct()
{
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn)
{
$this->clients->attach($conn); # store new con to send msg later
echo 'New Connection! '. $conn->resourceId ."\n";
}
public function onMessage(ConnectionInterface $from, $msg)
{
$numRecv = count($this->clients) - 1;
echo sprintf(
'Connection %d sending message "%s" to %d other connection%s'. "\n",
$from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's'
);
foreach ($this->clients as $client)
{
if ($from !== $client) {
$client->send($msg); # sender is not receiver, send to each client connected
}
}
}
public function onClose(ConnectionInterface $conn)
{
$this->clients->detach($conn); # con is closed, rm it, can no longer send it a msg
echo 'Connection '. $conn->resourceId .' has disconnected'. "\n";
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
echo 'An error has occurred: '. $e->getMessage() ."\n";
$conn->close();
}
}
和我的聊天 server.php
<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use App\Chat;
require dirname(__DIR__). '/vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8080
);
$server->run();
项目链接:http://socketo.me/
我围绕这些数字做了一些数学运算:
2195683 - 123384 = 2072299
difference between 14:03:13 and 14:19:24 is 16 minutes and 11 seconds
不知何故,16 分 11 秒必须大致等于 2072299
大更新
好的,所以我比较了 4 个新日期:
Thu 06 Sept 2018 15:26:40
Thu 06 Sept 2018 15:31:03
Thu 06 Sept 2018 16:26:40
Thu 06 Sept 2018 16:31:03
我有理论认为时间可能从某个时间开始,所以
26:40
将始终与每小时的 X 微秒相同。结果如下:Thu 06 Sept 2018 15:26:40
40491Thu 06 Sept 2018 15:31:03
303762Thu 06 Sept 2018 16:26:40
1351367Thu 06 Sept 2018 16:31:03
1614388
我决定找出它们之间的区别,它们都相隔 4 分 23 秒,但是这样做:
303762 - 40491 = 263271
和
1614388 - 1351367 = 263021
相差250。
我还比较了两者相隔一个小时的时间:
1351367 - 40491 = 1310876
和
1614388 - 303762 = 1310626
这又是 250 的差异。
所以没有多少时间是相同的,这让我怀疑它确实来自某个时间而不是现在。
然后我决定以 1s 的间隔进行:
21 = 2232366
22 = 2233365
23 = 2234333
正如人们所期望的那样,前 4 位数字递增(以 1 秒为单位),但随后变为 66、65、33……这是如何生成的?
谢谢,
最佳答案
感谢@Michel 的评论,我设法弄明白了。
我去了 timeago jquery 插件项目页面,它显示它来自连接,正如 Michel 所说。
它从服务器连接时间开始 - 为了测试我做了:
booted the script at 9:08:00
user 1 connect at 9:09:00
user 2 connect at 9:12:00
message 1 at 9:19:00
message 2 at 9:20:00
消息 1 返回时间戳:653905
消息 2 返回的时间戳为:713738
将毫秒转换为分钟时,第一个产生 10 分钟,后者返回 11 分钟。
看起来它以毫秒为单位,但由于使用 timeago,它从服务器开始 - 而不是 1970 年 1 月 1 日。
在文档中绝对应该注意的事情,但这是一个单独的问题。
关于php - 什么格式是 5/6 位时间戳,我该如何转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52204565/