How to calculate milliseconds?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
polk
Forum Newbie
Posts: 8
Joined: Tue Dec 01, 2009 3:46 pm

How to calculate milliseconds?

Post by polk »

Hi everyone,

I have this:

Code: Select all

<?php
 
$start_time = microtime(true);
 
// Do something...
 
$end_time = microtime(true);
 
echo 'Script executed in '. round(($end_time - $start_time), 3) . 'seconds.';
 
 
Now how could I get milliseconds? Since there are 1000 milliseconds in a second, I tried to divide by 1000 but I'm not sure of the result....... :?

Thanks again :oops:
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: How to calculate milliseconds?

Post by AlanG »

Code: Select all

function utime() {
    return reset(explode(' ', microtime()));
}
 
$start = utime();
 
$end = utime();
 
$difference = $end - $start;
:)

Explanation:

You'll notice if you print the result of the microtime function (echo microtime();), it returns a string such as "0.31732900 1259713502".
The first string is the microseconds. So all I did was 'explode' it into an array and return the first index. :)
polk
Forum Newbie
Posts: 8
Joined: Tue Dec 01, 2009 3:46 pm

Re: How to calculate milliseconds?

Post by polk »

Thank you AlanG for your answer!

But the result of this is in microseconds (there are 1 million microseconds in a second) and I'm trying to get milliseconds (1000 millisecond in a second) with a clean 3 digits result (from 000 to 999)... I can't figure it out :?

Thanks again, your help is really appreciated!
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: How to calculate milliseconds?

Post by AlanG »

Whoops! :)

You can use the round function to knock off a few digits.

Code: Select all

 
function msec() {
    $uTime = reset(explode(' ', microtime()));
    $mSec = round($uTime, 3); // If you don't want a decimal you could use ceil($uTime * 1000);
 
    return $mSec;
}
 
polk
Forum Newbie
Posts: 8
Joined: Tue Dec 01, 2009 3:46 pm

Re: How to calculate milliseconds?

Post by polk »

no no this is still microseconds :mrgreen: not milliseconds :P
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: How to calculate milliseconds?

Post by AlanG »

[POST DELETED OUT OF SHAME]

Basic maths is obviously a weakness of mine. :( lol

Yeah John Cartwright is probably right (I guess! I'm no authority on this lol)
Last edited by AlanG on Tue Dec 01, 2009 8:51 pm, edited 2 times in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: How to calculate milliseconds?

Post by John Cartwright »

Code: Select all

list($usec, $sec) = explode(" ",microtime());
$milliseconds = ((float)$usec/1000) + (float)$sec;
polk
Forum Newbie
Posts: 8
Joined: Tue Dec 01, 2009 3:46 pm

Re: How to calculate milliseconds?

Post by polk »

Thanks a lot guys, but it doesn't work... :(

Code: Select all

 
<?php
 
function msec() {
    //$uTime = reset(explode(' ', microtime()));
    //$mSec = ceil($uTime * 1000); // also tried with round($uTime, 3);
 
    list($usec, $sec) = explode(" ",microtime());
    $mSec = ((float)$usec/1000) + (float)$sec; 
 
    return $mSec;
}
 
$start_time = msec();
 
// Do something...
 
$end_time = msec();
 
echo 'Execution time: ' . ($end_time - $start_time) . ' milliseconds.';
 
Is there something wrong with my implementation?
I get crazy results like: 7.4148178100586E-5 :dubious:

Thanks again for your time and patience!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: How to calculate milliseconds?

Post by John Cartwright »

The result you were getting is called scientific notation. It's a way of representing an abysmally large (or small) numbers. However, I forgot to multiply the seconds by 1000 in my example.
polk
Forum Newbie
Posts: 8
Joined: Tue Dec 01, 2009 3:46 pm

Re: How to calculate milliseconds?

Post by polk »

Thanks for your reply.

Code: Select all

    function millisecond() {
        list($usec, $sec) = explode(" ",microtime());
        $milliseconds = ((float)$usec/1000) + ((float)$sec*1000); 
 
        return $milliseconds;
    }
 
doesn't seem to work either..
I'm not sure if I understand the logic behind this...

Anyway I don't understand why this doesn't work:

Code: Select all

 
$start_time = microtime(true); 
// Do something...
$end_time = microtime(true);
  
echo 'Script executed in: <br />'
       . round( (($end_time - $start_time)/60), 3) . 'minutes. <br />'
       . round( ($end_time - $start_time), 3) . 'seconds. <br />'
       . round( (($end_time - $start_time)*1000), 3) . 'milliseconds. <br />'
       . round( (($end_time - $start_time)*1000000), 3) . 'microseconds.';
 
 
Why the result doesn't make sense? :?: :|

I would really like to stay with the simple structure of time capture at two different moments, and then calculate the way we want to convert it to the unit we want... this should be the right way to do it, no?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How to calculate milliseconds?

Post by pickle »

What result are you getting? It makes sense to me when I used test numbers:

Code: Select all

$start_time = 1.0;
$end_time = 122.11111111;
  
 echo 'Script executed in: <br />'
        . round( (($end_time - $start_time)/60), 3) . 'minutes. <br />'
        . round( ($end_time - $start_time), 3) . 'seconds. <br />'
        . round( (($end_time - $start_time)*1000), 3) . 'milliseconds. <br />'
        . round( (($end_time - $start_time)*1000000), 3) . 'microseconds.';

Code: Select all

Script executed in:
2.019minutes.
121.111seconds.
121111.111milliseconds.
121111111.11microseconds.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: How to calculate milliseconds?

Post by Apollo »

Polk, try this function:

Code: Select all

function msec() 
{
    list($usec, $sec) = explode(' ',microtime());
    return intval(($usec+$sec)*1000.0);
}
This returns the number (integer) of milliseconds since 1-1-1970.

Note that on 32-bit systems, you'll get overflow (because nowadays it's been more than 2³² millisec since 1-1-1970) so the results will only make sense if you subtract them from eachother and use the difference.

For example:

Code: Select all

$start_time = msec();
// ...do something that takes 1.5 seconds...
$stop_time = msec();
$duration = $start_time - $stop_time;
// Hooray, $duration is now approx. 1500
polk
Forum Newbie
Posts: 8
Joined: Tue Dec 01, 2009 3:46 pm

Re: How to calculate milliseconds?

Post by polk »

Thanks everyone for your help!
pickle wrote:What result are you getting? It makes sense to me when I used test numbers:
Thanks everyone for your help!

Code: Select all

$start_time = 1.0;
$end_time = 122.11111111;
  
 echo 'Script executed in: <br />'
        . round( (($end_time - $start_time)/60), 3) . 'minutes. <br />'
        . round( ($end_time - $start_time), 3) . 'seconds. <br />'
        . round( (($end_time - $start_time)*1000), 3) . 'milliseconds. <br />'
        . round( (($end_time - $start_time)*1000000), 3) . 'microseconds.';

Code: Select all

Script executed in:
2.019minutes.
121.111seconds.
121111.111milliseconds.
121111111.11microseconds.
but you've put these values manually, the question is how to get them automatically? :?
Apollo wrote:Polk, try this function:

Code: Select all

function msec() 
{
    list($usec, $sec) = explode(' ',microtime());
    return intval(($usec+$sec)*1000.0);
}
This returns the number (integer) of milliseconds since 1-1-1970.

Note that on 32-bit systems, you'll get overflow (because nowadays it's been more than 2³² millisec since 1-1-1970) so the results will only make sense if you subtract them from eachother and use the difference.

For example:

Code: Select all

$start_time = msec();
// ...do something that takes 1.5 seconds...
$stop_time = msec();
$duration = $start_time - $stop_time;
// Hooray, $duration is now approx. 1500
Yes it works, thanks! but I'm still looking to understand why my example doesn't. And I'm still looking for a simpler method. :oops:

Thanks everyone!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How to calculate milliseconds?

Post by pickle »

I manually entered those numbers so the result would be easier to comprehend. You're almost certainly going to get some form of scientific notation as a result when doing this.

Your code probably works - you're just not understanding what it's outputing.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply