1

I created a method in php like this:`

function start(){
    echo"Stopwatch has started.<br/>";
    $this->startTime = mktime(date("H"), date("s"), date("m"), date("d"), date("Y"));
    $_SESSION["time"] = $this->starTime;
}

function stop(){
    echo"stopwatch stopped. <br/>";
    $this->endTime = mktime(date("H")-1, date("s"), date("m"), date("d"), date("Y"));
    echo"<b>Time Elapsed: </b>, date('H:i:s', $this->endTime-$_SESSION['time'])"; 
}

} `

my goal is to ask the stopwatch about the duration between start and stop. I have to show the duration, then I could use the stop watch multiple times, where the duration value each time I make it stop and start should be calculated properly. Can someone help me?

Merlin
  • 13
  • 1
  • 5
  • 1
    Please read https://stackoverflow.com/help/how-to-ask on how to ask better questions. What exactly does not work and what steps did you do to try and fix it? Also please do research on you topic, as this is a possible duplicate: https://stackoverflow.com/questions/8310487/start-and-stop-a-timer-php – Dollique Jan 16 '21 at 09:45

1 Answers1

5

Use hrtime() https://www.php.net/manual/en/function.hrtime.php

Here is a snippet:

$start=hrtime(true);
sleep(5);//this is what you are going to be measuring
$end=hrtime(true);
$eta=$end-$start;

echo $eta/1e+6; //nanoseconds to milliseconds
//5000.362419
Ziarek
  • 679
  • 6
  • 14
  • 1
    A disclaimer: this does not work in all (or any?) Windows operating systems. In regards to `hrtime()`, `microtime()`, etc. This comment is here for Windows dev users hosting code on linux systems. – GetSet Jan 16 '21 at 02:46
  • It works for me on both Win10 and linux - remember to wrap the code in php tags () and run on PHP version >= 7.3.0. What error are you getting? – Ziarek Jan 16 '21 at 04:12