Skip to content

Instantly share code, notes, and snippets.

@sbarrat
Last active December 17, 2015 16:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sbarrat/5642808 to your computer and use it in GitHub Desktop.
Save sbarrat/5642808 to your computer and use it in GitHub Desktop.
Clase PHP para sumar tiempos / PHP Class to add times
<?php
/**
* SumaTiempos.php Clase para sumar tiempos
*
* Esta clase sirve para teniendo un tiempo inicial y un Array con tiempos
* los suma y nos devuelve el tiempo final
*
* PHP Version 5.3
*
* @author Ruben Lacasa Mas <ruben@rubenlacasa.es>
* @copyright 2013 Ruben Lacasa Mas http://rubenlacasa.es
* @license http://creativecommons.org/licenses/by-nc-nd/3.0
* CC-BY-NC-ND-3.0
* @link https://gist.github.com/sbarrat/5642808
*/
class SumaTiempos
{
/**
* Inicializamos el array que almacenara los valores del tiempo
* @var Array
*/
private $tiempo = array();
/**
* Constructor de clase
*
* @param String $tiempo Tiempo que definimos
*/
public function __construct($tiempo = '00:00:00.00')
{
list(
$this->tiempo['horas'],
$this->tiempo['minutos'],
$this->tiempo['segundos']
) = explode(":", $tiempo);
}
/**
* Sumamos el tiempo a otro
*
* @param SumaTiempos $tiempo Tiempo que queremos agregar
*/
public function sumaTiempo(SumaTiempos $tiempo)
{
// Segundos
$this->tiempo['segundos']
= floatval($this->tiempo['segundos'])
+ floatval($tiempo->tiempo['segundos']);
$minutos = intval($this->tiempo['segundos'] / 60);
$this->tiempo['segundos'] -= $minutos * 60;
// Minutos
$this->tiempo['minutos']
= intval($this->tiempo['minutos'])
+ intval($tiempo->tiempo['minutos'] + $minutos);
$horas = intval($this->tiempo['minutos'] / 60);
$this->tiempo['minutos'] -= $horas * 60;
// Horas
$this->tiempo['horas']
= intval($this->tiempo['horas'])
+ intval($tiempo->tiempo['horas'] + $horas);
}
/**
* Formatea y devuelve el tiempo final
*
* @return String Tiempo final formateado
*/
public function verTiempoFinal()
{
return sprintf(
'%02s:%02s:%05s',
$this->tiempo['horas'],
$this->tiempo['minutos'],
$this->tiempo['segundos']
);
}
}
/**
* Ejemplo de uso
*/
/**
* Array de tiempos que queremos sumar
* @var Array
*/
$tiempos = array('10:21:54.35', '01:12:44.56', '20:01:12.93', '00:28:18.56');
// Inicializamos el tiempo
$tiempoInicial = new SumaTiempos();
// Recorremos los tiempos y los vamos sumando
foreach ($tiempos as $parcial) {
$tiempoInicial->sumaTiempo(new SumaTiempos($parcial));
}
// Mostramos el tiempo final
echo $tiempoInicial->verTiempoFinal(); // En este ejemplo 32:04:10.40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment