Search code examples
phptimestampratchet

What Format is a 5/6 Digit TimeStamp and how do I convert it?


Using Ratchet WebSocket and I'm trying to display the timestamp of the sent message. I console.log(e) inside of the onmessage callback and I get an object:

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

I thought it looked weird considering I sent it at 13:48. Weirdly, before, I sent a request within the last hour and it returned a 5 digit timeStamp which makes my researching that much harder.

I tried looking in Ratchet docs for the field but couldn't find anything. I instantly tried to see if it was a UNIX timestamp by converting it online but all the values return

1 Jan 1970 some:time

How can I convert the returned timeStamp to d/m/y H:i? I can't find anything on what format it may be in or how to convert.

Currently I have (in my jQuery):

new Date(data.timeStamp*1000)

but it returns the 1 Jan thing.

I've tried at two times:

Thursday 6th September 2018 14:03:(seconds was roughly 13)
Thursday 6th September 2018 14:19:24

This produced these timestamps (respectively):

0 1 2 3 3 8 4
2 1 9 5 6 8 3

(display was without spaces)

(note: leading 0 was not in output - just for column comparison)

I tried comparing the two, everything aside from Minute and second should be the same, yet - only two columns match.

How I possibly thought it could have been (but turns out it can't be based on how different the results are):

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

Here's my Chat.php class:

<?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();
        }
    }

and my chat-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();

link to project: http://socketo.me/

Some maths I've done around the figures:

2195683 - 123384 = 2072299

difference between 14:03:13 and 14:19:24 is 16 minutes and 11 seconds

Somehow 16 minutes and 11 seconds has to roughly equal 2072299

big update

Ok so I've compared 4 new dates:

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

I had theory that perhaps the time was going from a certain time, so 26:40 will always be the same as it's X microseconds from the hour. Here are the results:

Thu 06 Sept 2018 15:26:40
40491

Thu 06 Sept 2018 15:31:03
303762

Thu 06 Sept 2018 16:26:40
1351367

Thu 06 Sept 2018 16:31:03
1614388

I decided to work out the difference between them, all are 4mins and 23 seconds apart, however doing:

303762 - 40491 = 263271

and

1614388 - 1351367 = 263021

which has a difference of 250.

I also compared an hour apart for both:

1351367 - 40491 = 1310876

and

1614388 - 303762 = 1310626

which again has a difference of 250.

So no amount of time is the same, which makes me suspect that it does go from a certain time rather than time now.

I then decided to do it a 1s interval:

21 = 2232366

22 = 2233365

23 = 2234333

the first 4 digits increment as one might expect (in 1s) but then it goes 66, 65, 33 ... how is this being generated?

Thanks,


Solution

  • Thanks to @Michel's comment I managed to figure it out.

    I went to the timeago jquery plugin project page and it showed that it goes from the connection, just as Michel said.

    It goes from the server connection time - to test I did:

    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

    Message 1 returned timestamp of: 653905

    Message 2 returned timestamp of: 713738

    When converting milliseconds to minutes the first yields 10 minutes and the latter returned 11 minutes.

    Looks like it is in ms but because of the use of timeago it goes from the server start - not 1 Jan 1970.

    Something that should definitely be noted in the docs but that's a separate issue.