PHP start stop timer

Posted on 2004-11-28
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.
Question by:thehaze
    LVL 48

    Assisted Solution

    See (have a look at the comments for function which convert second into hour, minutes ...)

    For your case a very simple script would look like:
    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">
    LVL 4

    Accepted Solution

    I hope the functions below may help you..

    // 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() {
            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) {
         return $this->getElapsedTime($decimals);

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Article by: kevp75
    Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
    When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    732 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now