代码之家  ›  专栏  ›  技术社区  ›  treyBake user1850175

5/6位的时间戳是什么格式?如何转换?

  •  1
  • treyBake user1850175  · 技术社区  · 6 年前

    使用RatchetWebSocket,我试图显示发送消息的时间戳。我 console.log(e) 在onmessage回调中,我得到一个对象:

    {
        //... rest of the keys
        timeStamp: 353443
    }
    

    我试着在Ratchet文档中查找这个领域,但什么也找不到。我立即尝试通过在线转换来查看它是否是UNIX时间戳,但所有的值都返回了

    1970年1月1日一些:时间

    如何将返回的时间戳转换为 d/m/y H:i

    目前我有(在jQuery中):

    new Date(data.timeStamp*1000)
    

    但它返回1月1日的东西。

    我试过两次:

    2018年9月6日星期四14:03:(秒约为13)

    这产生了这些时间戳(分别):

    0 1 2 3 3 8 4

    (显示没有空格)

    (注意:前导0不在输出中-仅用于列比较)

    我试着比较这两个,除了分和秒之外,所有的东西都应该是一样的,但是只有两列是匹配的。

    星期四可能是3或4,这取决于编码器是否将星期天或星期一设置为0

    如果jan=0,Sept可能是8

    2018年可能是18岁

    14可能是2,如果12小时,标志设置为am/pm

    这是我的聊天室.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();
            }
        }
    

    <?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/

    我围绕这些数字做了一些数学计算:

    不知何故,16分11秒等于207229秒

    大更新

    好吧,我比较了4个新的日期:

    2018年9月6日星期四15:31:03

    2018年9月6日星期四16:26:40

    2018年9月6日星期四16:31:03

    26:40 总是和小时的X微秒一样。结果如下:

    2018年9月6日星期四15:26:40
    40491


    303762

    2018年9月6日星期四16:26:40

    2018年9月6日星期四16:31:03
    1614388

    303762 - 40491 = 263271

    两者相差250。

    我还比较了两人相隔一小时的情况:

    1351367 - 40491 = 1310876

    1614388 - 303762 = 1310626

    两者相差250。

    然后我决定每隔1秒做一次:

    23 = 2234333

    前4位数字按预期递增(1s内),但接着是66、65、33。。。这是如何产生的?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Community Mike Causer    4 年前

    多亏了@Michel的评论,我终于弄明白了。

    我去了timeagojquery插件项目页面,它显示了它来自连接,正如Michel所说。

    在9:08:00启动了脚本

    9:20:00留言2

    消息1返回时间戳:653905

    消息2返回时间戳:713738

    当将毫秒转换为分钟时,第一个得到10分钟,而后者返回11分钟。