Hi, Guest ~ Login or Register

Ze Timer Class

Ze Timer Class

Listed In Uncategorized » — Viewing Full Tutorial
Ever seen pages like "This page loaded in x.xxxxxxx seconds"?!

I'll teach you (or more or less show you) how to do that!

We start of with a (simple?) class!

<?

class timer
{

  var $name;
  var $start;
  var $end;
  var $totaltime;
  var $formatted;

  // Starts the timer
  function timer()
  {
    $this->add();
  }

  function add()
  {
    if( !$this->start ) {
      $mtime1 = explode( " ", microtime() );
      $this->start = $mtime1[1] + $mtime1[0];
    }
  }

  // Return the timer time
  function gettime()
  {
    if( $this->end )
    {
      return $this->totaltime;
    }
    elseif( $this->start &amp;&amp; !$this->end )
    {
      $mtime2 = explode( " ", microtime() );
      $currenttime = $mtime2[1] + $mtime2[0];
      $totaltime = $currenttime - $this->start;
      return $this->format( $totaltime );
    }
    else
    {
      return false;
    }
  }

  // Stops the timer
  function stop()
  {
    if( $this->start )
    {
      $mtime2 = explode( " ", microtime() );
      $this->end = $mtime2[1] + $mtime2[0];
      $totaltime = $this->end - $this->start;
      $this->totaltime = $totaltime;
      $this->formatted = $this->format( $totaltime );
      return $this->formatted;
    }
  }

  // Removes the timer
  function remove()
  {
    $this->name = "";
    $this->start = "";
    $this->end = "";
    $this->totaltime = "";
    $this->formatted = "";
  }

  // Formats the string $string
  function format($string)
  {
    return number_format( $string, 7 );
  }

}

?>


So there, we have our class.

Now we want to start the timer, so do this;

<?

// Our time foo.
$timer = new timer();
$time = $timer->stop();
$ze_time += $timer->totaltime;
$timer->remove();

// Show the result
echo $ze_time;

?>


Hopefully you understand it, if not PM moi.

Sorry I can't explain it, as I said in my previous tutorial- in a rush. :((

Thanks!

Working Beta

  1. The Forums
    These are mostly functional. If you see any weird bugs, post a thread about it and an administrator will do something.
  2. Tutorial Writing
    You can now submit tutorials to the brand new management system.
  3. Tutorials Home
    View tutorials by categories and search for them here.
  4. Shoutbox
    See below. Registered users only!

Register

Newest User

Say hi to kapricon! kapricon joined on Wednesday, 24th December.

Sponsor

Shoutbox