Problem :-

We are trying to convert timestamp 2016-02-11 03:31:18 to pubnub timestamp of 17-digit precision unix time (UTC) something like 13406746780720711 given in reference url given by pubnub

function parseDateTime(s) {
  var b = s.split(/\D/);
  return new Date(b[0],b[1]-1,b[2],b[3],b[4],b[5])
}
date = new Date(parseDateTime("2015-02-11 02:10:54") / 10000);
console.log(date.getTime());//142360085

Above example gives output 142360085 which is 10 characters where pubnub asks for 17 digit timestamp.
Reason behind doing this is i want to fetch unread messages of particular user and send an email at EOD via email.

After converting mytimestamp to 17-digit precision unix time (UTC) i will pass it to pubnub history function and get unread messages.

Solution :- 
Except for PHP. ❌ PHP ❌ does not support this level of integer precision! You can instead use String Concatenation and Coercion.

$tt = $unixtime_milliseconds . "0000";

Your unix time must be represented in millisecond precision, no fractions / no floats.
example using JavaScript.

// Vars
var timestamp   = +new Date;
var unix_time   = document.getElementById("unix-timestamp");
var pubnub_time = document.getElementById("pubnub-timetoken");

// Conversion
function unix_to_pubnub(time) {
    return time * 10000;
}

// Update Time
unix_time.innerHTML   = timestamp;
pubnub_time.innerHTML = unix_to_pubnub(timestamp);
<span id="unix-timestamp"></span> - Unix Timestamp <br>
<span id="pubnub-timetoken"></span> - PubNub Timetoken

You may also like

Leave a Reply