Skip to content

Instantly share code, notes, and snippets.

@lelotnk
Created July 18, 2013 19:32
Show Gist options
  • Save lelotnk/6032287 to your computer and use it in GitHub Desktop.
Save lelotnk/6032287 to your computer and use it in GitHub Desktop.
Calcular Diferença de Horas entre Datas
static function getIntervalUnits($interval, $unit)
{
// Day
$total = $interval->format('%a');
if ($unit == 'days')
return $total;
//hour
$total = ($total * 24) + ($interval->h );
if ($unit == 'hours')
return $total;
//min
$total = ($total * 60) + ($interval->i );
if ($unit == 'minutes')
return $total;
//sec
$total = ($total * 60) + ($interval->s );
if ($unit == 'seconds')
return $total;
return false;
}
$dataEntrada = '18/07/2013 15:00';
$dataSaida = '23/07/2013 23:00';
$format = 'd/m/Y H:i';
$timeZone = new DateTimeZone('America/Sao_Paulo');
$entrada = DateTime::createFromFormat($format, $dataEntrada, $timeZone);
// echo $entrada->format('Y-m-d');
$saida = DateTime::createFromFormat($format, $dataSaida, $timeZone);
// echo $saida->format('Y-m-d');
$interval = $entrada->diff($saida);
echo $interval->format('%a dias and %h horas') . '<br />';
$diasTotal = $interval->days;
$horas = $interval->h;
$horasTotal = ($diasTotal * 24) + $horas;
echo 'Total de ' . $horasTotal . ' horas.';
echo '<br />';
// var_dump($interval);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment