| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- /**
- * Link : http://www.phpcorner.net
- * User : qingbing<780042175@qq.com>
- * Date : 2018-10-28
- * Version : 1.0
- */
- namespace Helper;
- class Timer
- {
- static private $_timeStore = []; // 时间记录栈
- /**
- * 开启一个 TIMER
- * @param string $type
- * @return string
- */
- static public function begin($type = 'app')
- {
- if (isset(self::$_timeStore[$type])) {
- return self::$_timeStore[$type];
- }
- return self::$_timeStore[$type] = self::microTime(false);
- }
- /**
- * 获取一个 TIMER 存活的时间
- * @param string $type
- * @return string
- * @throws Exception
- */
- static public function end($type = 'app')
- {
- if (!isset(self::$_timeStore[$type])) {
- throw new Exception(str_cover('没有"{type}"对应的 timer', [
- '{type}' => $type,
- ]));
- }
- $e = self::microTime(false);
- $s = self::$_timeStore[$type];
- unset(self::$_timeStore[$type]);
- return sprintf('%.6f', $e - $s);
- }
- /**
- * 获取当前的浮点型时间
- * @param bool $isFormat
- * @return string
- */
- static public function microTime($isFormat = true)
- {
- if ($isFormat) {
- list($microSec, $sec) = explode(" ", microtime());
- return Format::datetime($sec) . ' ' . substr($microSec, 2, 6);
- } else {
- return microtime(true);
- }
- }
- }
|