Ze Timer Class
Listed In Uncategorized » — Viewing Full TutorialI'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 && !$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!
