Create an Epoch time string with milliseconds

Some API requires epoch time with milliseconds which PHP time() function does produce as default. And microtime() function divides up to two time values if not use flag. (If there’s an additional flag, it may be better?) The following is a couple of walk-arounds. NOTE: The first one is what I’ve found on StackOverflow.

function getEpochTimeWithMilsec() {
  $mt = explode(' ', microtime());
  return ((int)$mt[1]) * 1000 + ((int)round($mt[0] * 1000));
}
function getEpochTimeWithMilsec() {
  $mt = microtime(true) * 1000;
  return (int)round($mt);
}

 

Leave a comment