?
Solved

PHP start stop timer

Posted on 2004-11-28
2
Medium Priority
?
1,981 Views
Last Modified: 2012-06-27
I am trying to code a simple stopwatch using PHP. I have a start and a stop button, and the resulting time is shown on the page. (in hours, mins and seconds.)
Does PHP have a built in timer function that can be used.?
Are there any scripts already available.?
All suggestions are welcome.
0
Comment
Question by:thehaze
2 Comments
 
LVL 48

Assisted Solution

by:hernst42
hernst42 earned 400 total points
ID: 12690742
See http://de3.php.net/time (have a look at the comments for function which convert second into hour, minutes ...)

For your case a very simple script would look like:
<?php
if(!empty($_POST['stop'])) {
    // elapsed time in seconds
    $etime=time() - $_POST['starrtime'];
    echo "elapsed time $etime<br>";
}
?>
<form method="post">
<input type="hidden" name="starttime" value="<?php echo !empty($_POST['start']) ? time() : $_POST['starttime']; ?>">
<input type="submit" name="start" value="start">
<input type="submit" name="stop" value="stop">
</from>
0
 
LVL 4

Accepted Solution

by:
ramu_src2k earned 600 total points
ID: 12698705
Hi,
I hope the functions below may help you..


<?php  
// Use:
////  Create a timer object.  At creation, Start time is recorded
// $t1 = new StopWatch();
// // When code or section of code is complete, stop the timer.
// $t1->Stop();
// // Display the Elapsed Time in seconds.  Defaults to 2 decimal places
// // or override the display by passing a parameter.
// $t1->getElapsedTime(4);
/************************************************************************/
class StopWatch {
    var $starttime;
    var $endtime;
     
    // Method Start
    // Purpose: Start Timer.
    function Start() {
        $this->starttime = microtime();
           return true;
    }
     
    // Constructor StopWatch.  
    // Purpose: Start Timer.
       function StopWatch() {
           $this->Start();
        return true;
    }
     
    // Method Stop
    // Purpose: Stop Timing.
    function Stop() {
          $this->endtime = microtime();
        return true;
      }
     
    // Method getElapsedTime
    // @decimals Number of decimals places to display
    // Returns string
    // Purpose: Display elapsed time in seconds to precision passed in optional parameter
       function getElapsedTime($decimals=2) {
        // $decimals will set the number of decimals you want for your milliseconds.
           // format start time
        $decimals = intval($decimals);
        if ($decimals > 8)
          $decimals = 8;
        if ($decimals < 0)
          $decimals = 0;
        $endtime = explode(" ", $this->endtime);
           $endtime = (float)$endtime[1] + (float)$endtime[0];
           $starttime = explode (" ", $this->starttime);
           $starttime = (float)$starttime[1] + (float)$starttime[0];
        return number_format($endtime - $starttime, $decimals);
   }
       // Member Function: Stop_getElapsedTime
    // @decimals Number of decimals places to display
    // Returns string
    // Purpose: Stop the timer and Display elapsed time in seconds to precision  passed in optional parameter
   function Stop_getElapsedTime($decimals=2) {
     $this->Stop();
     return $this->getElapsedTime($decimals);
   }
}
?>
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What You Need to Know when Searching for a Webhost Provider
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question