ollydbg
ollydbg

Reputation: 3525

How to get current time in ms in PHP?

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

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

Is that possible in PHP?

Upvotes: 2

Views: 29171

Answers (6)

NetAdapt
NetAdapt

Reputation: 816

Since PHP 7.3 hrtime has been available.

This means you can use high resolution timers in nanoseconds without any of the issues of microtime. (e.g. System clock changing between tests)

So, you can now get ms reliably on 64bit platforms with :

intval( hrtime(true) / 1000000 ); //Convert nanosecond to ms

Format as described in earlier answers.

Upvotes: 1

Thinh Phan
Thinh Phan

Reputation: 95

This is my way

list($usec, $sec) = explode(" ", microtime());
$time =  date("Y-m-d H:i:s:",$sec).intval(round($usec*1000));
echo $time;

Upvotes: 1

Spudley
Spudley

Reputation: 168843

Use the microtime() function.

See the manual page here: http://php.net/manual/en/function.microtime.php

[EDIT] To get the output in year/month/day/hour/minutes/seconds/ms as requested:

Try something like this:

list($usec, $sec) = explode(" ", microtime());
$output = date('Y/m/d H:i:s',$sec). " /" . $usec;

Again, see the manual page for more details on how microtime() works.

Upvotes: 2

WolfRevoKcats
WolfRevoKcats

Reputation: 294

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

Upvotes: 10

nnevala
nnevala

Reputation: 5997

Take a look at php's microtime function.

Upvotes: 0

ThiefMaster
ThiefMaster

Reputation: 318748

Use microtime and convert it to milliseconds:

$millitime = round(microtime(true) * 1000);

Upvotes: 8

Related Questions