[ Index ]

PHP Cross Reference of MyBB

title

Body

[close]

/inc/ -> class_timers.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.6
   4   * Copyright 2010 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://mybb.com
   7   * License: http://mybb.com/about/license
   8   *
   9   * $Id: class_timers.php 5828 2012-05-08 16:06:16Z Tomm $
  10   */
  11  
  12  class timer {
  13      
  14      /**
  15       * The timer name.
  16       *
  17       * @var string
  18       */
  19      public $name;
  20      
  21      /**
  22       * The start time of this timer.
  23       *
  24       * @var int
  25       */
  26      public $start;
  27      
  28      /**
  29       * The end time of this timer.
  30       *
  31       * @var int
  32       */
  33      public $end;
  34      
  35      /**
  36       * The total time this timer has run.
  37       *
  38       * @var int
  39       */
  40      public $totaltime;
  41      
  42      /**
  43       * The formatted total time this timer has run.
  44       *
  45       * @var string
  46       */
  47      public $formatted;
  48  
  49      /**
  50       * Constructor of class.
  51       *
  52       */
  53  	function __construct()
  54      {
  55          $this->add();
  56      }
  57      
  58      /**
  59       * Starts the timer.
  60       *
  61       */
  62  	function add()
  63      {
  64          if(!$this->start) 
  65          {
  66              $this->start = microtime(true);
  67          }
  68      }
  69  
  70      /**
  71       * Gets the time for which the timer has run up until this point.
  72       *
  73       * @return string|boolean The formatted time up until now or false when timer is no longer running.
  74       */
  75  	function getTime()
  76      {
  77          if($this->end) // timer has been stopped
  78          {
  79              return $this->totaltime;
  80          }
  81          elseif($this->start && !$this->end) // timer is still going
  82          {
  83              $currenttime = microtime(true);
  84              $totaltime = $currenttime - $this->start;
  85              return $this->format($totaltime);
  86          }
  87          else
  88          {
  89              return false;
  90          }
  91      }
  92      
  93      /**
  94       * Stops the timer.
  95       *
  96       * @return string The formatted total time.
  97       */
  98  	function stop()
  99      {
 100          if($this->start)
 101          {
 102              $this->end = microtime(true);
 103              $totaltime = $this->end - $this->start;
 104              $this->totaltime = $totaltime;
 105              $this->formatted = $this->format($totaltime);
 106              return $this->formatted;
 107          }
 108      }
 109      
 110      /**
 111       * Removes the timer.
 112       *
 113       */
 114  	function remove()
 115      {
 116          $this->name = "";
 117          $this->start = "";
 118          $this->end = "";
 119          $this->totaltime = "";
 120          $this->formatted = "";
 121      }
 122      
 123      /**
 124       * Formats the timer time in a pretty way.
 125       *
 126       * @param string The time string.
 127       * @return The formatted time string.
 128       */
 129  	function format($string)
 130      {
 131          return number_format($string, 7);
 132      }
 133  }
 134  ?>


Generated: Tue Oct 8 19:19:50 2013 Cross-referenced by PHPXref 0.7.1