如何在PHP中以ms为单位获取当前时间? [英] How to get current time in ms in PHP?

查看:75
本文介绍了如何在PHP中以ms为单位获取当前时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想得到的就像time()一样,但以毫秒为单位应该是准确的:

What I want to get is the very like time(),but should be accurate in ms:

2010-11-15 21:21:00:987

在PHP中有可能吗?

推荐答案

function udate($format, $utimestamp = null) {
  if (is_null($utimestamp))
    $utimestamp = microtime(true);

  $timestamp = floor($utimestamp);
  $milliseconds = round(($utimestamp - $timestamp) * 1000000);

  return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp);
}

echo udate('Y-m-d H:i:s:u'); // 2010-11-15 21:21:00:987

这篇关于如何在PHP中以ms为单位获取当前时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆