Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subtract microtime and display date with milliseconds in php?

Tags:

php

microtime

How to subtract microtime and display date with milliseconds in php ?

For example: I have set end date and time

$endtime = 2012-02-21 10:29:59;

then I have current date or start date with converted from microtime

$starttime = 2012-02-21 10:27:59.452;

function getTimestamp()
{
        $microtime = floatval(substr((string)microtime(), 1, 8));
        $rounded = round($microtime, 3);
        return date("Y-m-d H:i:s") . substr((string)$rounded, 1, strlen($rounded));
}

echo getTimestamp(); //sample output 2012-02-21 10:27:59.452

Now I want to subtract: $finaldate = $endtime - $starttime;

I want my output to be like this: 00:00:02.452

like image 944
wow development Avatar asked Feb 21 '12 02:02

wow development


People also ask

What is Microtime true in PHP?

PHP microtime() function returns the current Unix timestamp. By default this returns a string value in the form msec sec. If you pass the boolean value true as a parameter to this method, it returns the current time in seconds since the Unix epoch accurate to the nearest microsecond.

What is a Microtime?

Definition of microtime : a very short interval of time (as 0.01 millionth of a second) microtime photography.


2 Answers

You need to use microtime for the start/end values, and only format it for display at the end.

// Get the start time in microseconds, as a float value
$starttime = microtime(true);

/************/
/* Do stuff */
/************/

// Get the difference between start and end in microseconds, as a float value
$diff = microtime(true) - $starttime;

// Break the difference into seconds and microseconds
$sec = intval($diff);
$micro = $diff - $sec;

// Format the result as you want it
// $final will contain something like "00:00:02.452"
$final = strftime('%T', mktime(0, 0, $sec)) . str_replace('0.', '.', sprintf('%.3f', $micro));

Note: this is returning float values from microtime and using float arithmetic to simplify the math, so your numbers may be extremely slightly off due to the float rounding problem, but you are rounding the result to 3 digits in the end anyway, and minor fluctuations in processor timing are greater than floating point errors anyway, so this is not problem for you on multiple levels.

like image 132
Ben Lee Avatar answered Oct 07 '22 01:10

Ben Lee


Well phpmyadmin uses this a code like this to calculate the time that a query took. It's similar to your requirements:

//time before
list($usec, $sec) = explode(' ',microtime($starttime));
$querytime_before = ((float)$usec + (float)$sec);
/* your code */

//time after
list($usec, $sec) = explode(' ',microtime($endtime));
$querytime_after = ((float)$usec + (float)$sec);
$querytime = $querytime_after - $querytime_before;

I think this should work for you. You just have to figure your output format

like image 28
jribeiro Avatar answered Oct 07 '22 00:10

jribeiro