29

I have the store start time as microtime() and end time as microtime() into database.

Now I want to calculate how long the script takes the seconds/minutes/hours to execute.

How can I do in PHP?

svk
  • 4,513
  • 18
  • 63
  • 100

4 Answers4

57

basically, like this:

echo date("H:i:s",$endtime-$starttime);

$endtime-$starttime gives the duration in seconds, date is used to format the output. sounds like saving to and reading from a database isn't your problem here, so i left that out in my example. Note that you'll have to use microtime(true) to get this working, with the space-separated output of microtime() your can't do calculations that easy.

EDIT: you could do all the calculation on your own, too. it's just basic math like this:

$duration = $endtime-$starttime;
$hours = (int)($duration/60/60);
$minutes = (int)($duration/60)-$hours*60;
$seconds = (int)$duration-$hours*60*60-$minutes*60;
danronmoon
  • 3,814
  • 5
  • 34
  • 56
oezi
  • 51,017
  • 10
  • 98
  • 115
  • 7
    Your first example won't always work. `microtime` returns a unix timestamp, which obviously starts at 1/1/70 00:00:00 UTC. If `$endtime - $starttime` is greater than 24 hours, the diff timestamp returned would represent 2/1/70 01:00:00 UTC (assuming it's exactly 25 hours). Formatting this as `H:i:s` would give `01:00:00`, which appears to be one hour. – Adam Hopkinson Jun 24 '11 at 13:19
  • 4
    Also, might be worth pointing out in your second example that `microtime()` returns a string of `timestampmicroseconds` - you can't do maths on this, so you'll need to use `microtime(true)` to get a float – Adam Hopkinson Jun 24 '11 at 13:22
  • 1
    oh, thanks for the hint adam, i havn't thought of that, but you're right: if the duration could be more than 24 hours, my first solution wouldn't work - one would have to add "d" (and "m" and "Y" if needet) to output the number of days too or to do the calculation manually (like my second example is showing) – oezi Jun 24 '11 at 13:25
  • 2
    Yep, but `d` would only get you to 31 days! It's by-the-by, as the php execution time limit should *never* allow a script to run for that long – Adam Hopkinson Jun 24 '11 at 13:27
  • 1
    to your second comment: i added a hint to my post. i assume that vinothkumar uses microtime(true) to save a floating point number instead of a string, but he hasn't given exact information about that. – oezi Jun 24 '11 at 13:38
24

microtime to seconds or hours or min conversion?

microtime is the name of the php function that return a measure of time in microseconds and basically microseconds can be converted to:


    1 milliseconds = 1,000 microseconds
    1 second       = 1,000,000 microseconds
    1 minute       = 60,000,000 microseconds
    1 hour         = 3,600,000,000 microseconds


    or


    1 microsecond = 0.001 milliseconds
    1 microsecond = 0.000001 seconds
    1 microsecond = 0.0000000166666667 minutes
    1 microsecond = 0.000000000277777778 hours

Matt Fletcher
  • 8,182
  • 8
  • 41
  • 60
user2754369
  • 241
  • 2
  • 2
7
function formatPeriod($endtime, $starttime)
{

  $duration = $endtime - $starttime;

  $hours = (int) ($duration / 60 / 60);

  $minutes = (int) ($duration / 60) - $hours * 60;

  $seconds = (int) $duration - $hours * 60 * 60 - $minutes * 60;

  return ($hours == 0 ? "00":$hours) . ":" . ($minutes == 0 ? "00":($minutes < 10? "0".$minutes:$minutes)) . ":" . ($seconds == 0 ? "00":($seconds < 10? "0".$seconds:$seconds));
}
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
ahmed reda
  • 153
  • 2
  • 5
3

If you have a time A and a time B, in seconds, then the number of seconds between those two absolute times is:

B - A

If you want to format this number of seconds, you can use date to prettify it.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055