-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcronometro.php
More file actions
70 lines (61 loc) · 1.59 KB
/
cronometro.php
File metadata and controls
70 lines (61 loc) · 1.59 KB
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
<?php
/**
* Clase Cronometro
* Permite medir el tiempo transcurrido entre el arranque y la parada
* @author Sergio Fernandez-Miranda Longo
*/
class Cronometro {
private $inicio;
private $tiempo;
/**
* Constructor - Inicializa el tiempo a cero
*/
public function __construct() {
$this->tiempo = 0;
}
/**
* Marca el momento temporal en el que se inicia
*/
public function arrancar() {
$this->inicio = microtime(true);
}
/**
* Calcula el tiempo transcurrido desde el arranque
*/
public function parar() {
if (isset($this->inicio)) {
$fin = microtime(true);
$this->tiempo = $fin - $this->inicio;
}
}
/**
* Retorna el tiempo en formato mm:ss.s
* @return string Tiempo formateado
*/
public function mostrar() {
$minutos = floor($this->tiempo / 60);
$segundos = floor($this->tiempo % 60);
$decimas = floor(($this->tiempo - floor($this->tiempo)) * 10);
return sprintf("%02d:%02d.%d", $minutos, $segundos, $decimas);
}
/**
* Obtiene el tiempo transcurrido en segundos
* @return float Tiempo en segundos
*/
public function obtenerTiempo() {
return $this->tiempo;
}
/**
* Inicia el cronómetro (alias de arrancar para compatibilidad)
*/
public function iniciar() {
$this->arrancar();
}
/**
* Detiene el cronómetro (alias de parar para compatibilidad)
*/
public function detener() {
$this->parar();
}
}
?>