note 80869 added to function.microtime

From: Date: Tue, 05 Feb 2008 04:55:36 +0000
Subject: note 80869 added to function.microtime
Groups: php.notes 
Request: Send a blank email to php-notes+get-135780@lists.php.net to get a copy of this message
Here is my first object. I made a stopwatch that I think is pretty neat. It uses the microtime setting with the floating point enabled. I spent a lot of time working with the other version until it dawned on me that it is accurate to the thousandth of a second which is good enough for all of the sites that I will be working on for the time being. <?php //Details: // Started Stopped // --------------------------------------- // | 0 | 0 | = Has not been used yet, or has been reset // --------------------------------------- // | 0 | 1 | = Holding time, but paused // --------------------------------------- // | 1 | 0 | = Currently running // --------------------------------------- class Stopwatch { public function __construct() { $this->started = $this->stopped = $this->timer=0; } public function Start() { //leave the function because it is already running if($this->started) return; if(!($this->stopped)) { $this->started = true; $this->timer = microtime(true); } //stopwatch is currently stopped, begin tracking time again if($this->stopped) { $this->timer = microtime(true) - $this->timer; $this->started = true; $this->stopped = false; } } public function Stop() { //make sure that it is running before you stop it if(!$this->stopped && $this->started) { $this->timer = microtime(true) - $this->timer; $this->stopped = true; $this->started = false; } } public function Reset() { $this->timer = $this->started = $this->stopped = false; } public function Display() { //still running, use current time. if(!($this->stopped) && ($this->started)) $sec = microtime(true) - $this->timer; else if(!$this->stopped && !$this->started) $sec = 0; else $sec = $this->timer; echo "<br />Time: $sec seconds<br />"; } private $timer, $started, $stopped; } ?> ---- Server IP: 209.41.74.194 Probable Submitter: 76.202.218.80 ---- Manual Page -- http://www.php.net/manual/en/function.microtime.php Edit -- https://master.php.net/note/edit/80869 Del: integrated -- https://master.php.net/note/delete/80869/integrated Del: useless -- https://master.php.net/note/delete/80869/useless Del: bad code -- https://master.php.net/note/delete/80869/bad+code Del: spam -- https://master.php.net/note/delete/80869/spam Del: non-english -- https://master.php.net/note/delete/80869/non-english Del: in docs -- https://master.php.net/note/delete/80869/in+docs Del: other reasons-- https://master.php.net/note/delete/80869 Reject -- https://master.php.net/note/reject/80869 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#135780) next »