-
Notifications
You must be signed in to change notification settings - Fork 0
/
YeOldTimey.php
109 lines (102 loc) · 2.72 KB
/
YeOldTimey.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* Ye OldenTimey
*
* A stupid simple tool that helps with timing php scripts.
*
*
* @author Juan Orozco <juanthedesigner@gmail.com>
* @copyright 2012 Juan Orozco
*/
class YeOldenTimey
{
public $starttime;
public $endtime;
public $totaltime;
public $laptimes = array();
//starts timer on ini
public function __construct()
{
$this->startTimer();
}
private function startTimer( $reset = false )
{
$this->starttime = microtime(true);
if ( $reset == true ) $this->endtime = null;
}
private function endTimer()
{
$this->endtime = microtime(true);
}
//calculates start/end time difference
private function getTotalTime()
{
$this->endTimer();
$this->totaltime = $this->endtime - $this->starttime;
return $this->totaltime;
}
//records a lap time
private function setLapTime( $name = '' )
{
$time = microtime(true);
if ( $name == '' OR empty($name) ) $this->laptimes[]=$time;
else $this->laptimes[$name] = $time;
}
//gets lap time by name, if no name passed, returns all as array
private function getLapTime( $name = '' )
{
if ( $name == '' OR empty($name) ) return $this->laptimes;
else return $this->laptimes[ $name ];
}
//main lap time method
public function laps( $action = 'set', $name = '' )
{
switch( $action )
{
case 'get':
return $this->getLapTime($name);
break;
case 'set':
$this->setLapTime($name);
return true;
break;
case 'compare':
//compares to end time or to current time.
$laptime = $this->getLapTime($name);
$endtime = empty($this->endtime) ? microtime(true) : $this->endtime;
$totaltime = $endtime - $laptime;
return $totaltime;
break;
default:
// default to set
$this->setLapTime($name);
return true;
break;
}
}
//main timer method
public function timer( $action = 'lap', $force = false )
{
switch( $action )
{
case 'restart':
//restarts timer
$this->startTimer($force);
return true;
break;
case 'end':
$this->endTimer();
return true;
break;
case 'get':
return $this->getTotalTime();
break;
default:
//trigger a lap
$this->laps();
return true;
break;
}
}
}
?>