forked from geekgirljoy/PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppTimer.Class.php
166 lines (130 loc) · 4.44 KB
/
AppTimer.Class.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/*
* AppTimer.Class.php
* Joy Harvel
* 2018
*/
class AppTimer
{
/*///////////////////////////////////////////////////*/
/* AppTimer Properties */
/*///////////////////////////////////////////////////*/
private $start = NULL;
private $stop = NULL;
private $report = NULL;
/*/////////////////////////////////////////////////////////*/
/* Constructor */
/* */
/* Set $AutoStart to 1 or true to begin timer at creation */
/* */
/*/////////////////////////////////////////////////////////*/
function __construct($AutoStart = 0){
if($AutoStart == true){
$this->Start();
}
}
/*///////////////////////////////////////////////////*/
/* Destructor */
/*///////////////////////////////////////////////////*/
function __destruct() {
// No More AppTimer
}
/*///////////////////////////////////////////////////*/
/* Start() */
/*///////////////////////////////////////////////////*/
function Start(){
$this->start = microtime(true); // now
$this->stop = NULL; // set stop to NULL in case timer is used multiple times
}
/*///////////////////////////////////////////////////*/
/* Stop() */
/*///////////////////////////////////////////////////*/
function Stop($AutoReport = 0){
$this->stop = (microtime(true) - $this->start); // now - start
if($AutoReport == true){
return $this->Report();
}
}
/*///////////////////////////////////////////////////*/
/* Report() */
/*///////////////////////////////////////////////////*/
function Report( ){
// Timer has stopped
if($this->start != NULL && $this->stop != NULL){
$this->report = $this->stop;
}
else{ // Timer is still running or was never started
if($this->start != NULL){
$this->report = (microtime(true) - $this->start); // now - start
}
else{
return 'Timer was not started. Use Start() method.';
}
}
// Compute Report as Hours & or Minutes & or Seconds
if($this->report > 3600){ // Hours
$hours = round(($this->report / 60) / 60, 8);
if (is_float($hours)){ // Minutes
$minutes = '0' . strstr($hours, '.');
$minutes = $minutes * 60;
$hours = round($hours, 0);
if (is_float($minutes)){ // Seconds
$seconds = '0' . strstr($minutes, '.');
$seconds = round($seconds * 60, 4);
$minutes = round($minutes, 0);
$this->report = $hours . ' Hours ' . $minutes . ' Minutes ' . $seconds . ' Seconds';
}
else{
$this->report = $hours . ' Hours ' . $minutes . ' Minutes';
}
}
else{
$this->report = $hours . ' Hours';
}
}
elseif($this->report > 60){ // Minutes
$minutes = round($this->report / 60, 8);
if (is_float($minutes)){ // Seconds
$seconds = '0' . strstr($minutes, '.');
$seconds = round($seconds * 60, 4);
$minutes = round($minutes, 0);
$this->report = $minutes . ' Minutes ' . $seconds . ' Seconds';
}
else{
$this->report = $minutes . ' Minutes';
}
}
else{ // Seconds
$this->report = round($this->report, 4) . ' Seconds';
}
return $this->report;
}
/*///////////////////////////////////////////////////*/
/* CallbackTimer() */
/*///////////////////////////////////////////////////*/
function CallbackTimer($callback = NULL, $arguments = array()){
// if callback is array then it is object & method
//$callback[0] = object
//$callback[1] = method name
// if callback is string then it is a function name
//$callback = 'YourFunctionName'
//$arguments[0] = argument 0
//$arguments[1] = argument 1
//$arguments[n] = argument n
if($callback != NULL){
$this->Start();
call_user_func_array($callback, $arguments); // run callback
return $this->Stop(1); // Auto Report
}else{
return 'Missing Callback Name or Array.'; // Error
}
}
/*///////////////////////////////////////////////////*/
/* GetTime() */
/*///////////////////////////////////////////////////*/
function GetTime($duration_seconds){
$this->start = 1;
$this->stop = $duration_seconds + 1;
return $this->Report();
}
}